2011-05-03 17 views
0

谁能帮我找到内存分配和泄漏?谁能帮我找到内存分配和泄漏?

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    NSInteger rowIndex = [indexPath row];//row index  
    static NSString *NineCellIdentifier = @"NineCellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NineCellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NineCellIdentifier] autorelease]; 
    } 

NSNumber* aHeight = [heightArray objectAtIndex:rowIndex]; 
itemHeight = [aHeight integerValue]; 

NSInteger index = 0; 
for (int j=0; j< columnNum; j++) 
{ 
    CGFloat x, y, w, h; 
    x = 0; 
    y = 0; 
    w = itemWidth; 
    h = itemHeight; 

    index = rowIndex*columnNum + j; 
    if (index >= [itemList count]) 
    { 
     break; 
    } 
    //NSLog(@"index = %d, j= %d", index, j); 
    curItem = [itemList objectAtIndex:index]; 
    NSString *imageFile = [appDelegate.clientResourcesDir stringByAppendingPathComponent:curItem.pic]; 
    UIImage *image = [UIImage imageWithContentsOfFile:imageFile]; 
    NSInteger imageWidth = image.size.width; 
    NSInteger imageHeight = image.size.height; 
    if (imageWidth > itemWidth) 
    { 
     imageHeight = imageHeight*itemWidth/imageWidth; 
     imageWidth = itemWidth; 
    }  

    UIButton *itemBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)]; 
    [itemBtn setBackgroundColor:[UIColor clearColor]]; 
    [itemBtn setTag:index]; 
    [itemBtn addTarget:self action:@selector(goNextPageView:) forControlEvents:UIControlEventTouchUpInside]; 

    if ([curItem.text1 isEqualToString:@""]) 
    { 
     x = itemBtn.frame.origin.x + (itemWidth - imageWidth)/2; 
     y = itemBtn.frame.origin.y + (itemHeight - imageHeight)/2; 
    } 
    else 
    { 
     x = itemBtn.frame.origin.x + (itemWidth - imageWidth)/2; 
     y = itemBtn.frame.origin.y + (itemHeight - imageHeight - textSize - blankSize)/2; 
    }  

    //1 button 
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, imageWidth, imageHeight)]; 
    [button setBackgroundImage:image forState:UIControlStateNormal]; 
    [button setTag:index]; 
    [button addTarget:self action:@selector(goNextPageView:) forControlEvents:UIControlEventTouchUpInside]; 
    [itemBtn addSubview:button]; 
    [button release]; 

    //2 label 
    x = 0; 
    y = y + imageHeight + blankSize; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, itemWidth, textSize)]; 
    label.font = [UIFont boldSystemFontOfSize:textSize]; 
    [label setTextAlignment:UITextAlignmentCenter]; 
    [label setBackgroundColor:[UIColor clearColor]];   
    label.textColor = [UIColor colorWithRed:rf green:gf blue:bf alpha:1]; 
    label.lineBreakMode = UILineBreakModeWordWrap; 
    label.text = curItem.text1; 
    label.tag = -1;  
    [itemBtn addSubview:label]; 
    [label release]; 

    //itemBtn.contentMode = UIViewContentModeBottom; 
    CGRect frame = itemBtn.frame; 
    frame.origin.x = j * itemWidth; 
    frame.origin.y = 0; 
    itemBtn.frame = frame; 

    [cell addSubview:itemBtn];  
    [itemBtn release]; 
} 
return cell; 
} 

回答

0
  1. curItem已被释放,如果它有保有财产
比这

其他的我不能看到这段代码的任何泄漏。泄漏可能在其他地方。

0

也许我错过了一些东西,但是我看不到有什么东西会在技术上被认为是泄漏。我能看到的唯一问题是,每次单元格被绘制时,都会添加所有按钮,因此最终会出现大量标签和按钮,并且可能会导致内存不足。您可以通过每次删除所有子视图来解决此问题。当你这样做时,你可能想要将按钮添加到cell.contentView而不是仅仅给单元格,否则最终可能会删除不意味着的视图。

0

itemBtn正在泄漏。要弄清楚为什么会发生,你应该了解UITableView的工作原理。当它在单元格出现不可见的单元格时,即使它已经添加,它也会添加itemBtn。

你应该继承UITableViewCell并在init方法中添加itemBtn。在tableView:cellForRowAtIndexPath中:你应该只设置一些属性,不要在你的单元格中添加任何子视图。