谁能帮我找到内存分配和泄漏?
// 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 :(得分:0)
除此之外,我在这段代码中看不到任何泄漏。泄漏可能在别的地方。
答案 1 :(得分:0)
也许我错过了一些东西,但我看不到任何技术上被认为是漏洞的东西。我能看到的唯一问题是,每次绘制单元格时都会添加所有按钮,因此最终会出现大量标签和按钮,并且内存可能会耗尽。您可以通过每次删除所有子视图来解决此问题。执行此操作时,您可能希望将按钮添加到cell.contentView而不是仅添加到单元格,否则您最终可能会删除您不想要的视图。
答案 2 :(得分:0)
itemBtn正在泄露。要弄清楚它为什么会发生,你应该了解UITableView的工作原理。当它将细胞从当前不可见的单元格中取出时,它会添加itemBtn,即使它已被添加。
你应该继承UITableViewCell并在init方法中添加itemBtn。在tableView:cellForRowAtIndexPath中:您应该只设置一些属性,不要在单元格上添加任何子视图。