gpt4 book ai didi

iphone - UITableViewCell backgroundView 在不应该被重用时被重用

转载 作者:行者123 更新时间:2023-11-28 17:44:58 25 4
gpt4 key购买 nike

我想为我的表格 View 单元格添加自定义背景和选定的背景图像。目前看来,当单元格被重用时,背景图像会搞砸,顶部单元格将使用底部单元格图像,等等。

在这种情况下,我是否错误地重用了单元格?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UIImageView *linkAvailableImageView = nil;
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];

UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, tableView.bounds.size.width-20, 44)];
UIImageView *selectedBackgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, tableView.bounds.size.width-20, 44)];


// Asset
Asset *asset = nil;
asset = (Asset *)[items objectAtIndex:indexPath.row];

int count = [items count];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

if (indexPath.row == 0 && count > 1) {
backgroundImage.frame = CGRectMake(10, 0, tableView.bounds.size.width-20, 45);
backgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundTop.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
selectedBackgroundImage.frame = CGRectMake(10, -1, tableView.bounds.size.width-20, 45);
selectedBackgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSelectedTop.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
} else if (indexPath.row == count-1 && count > 1) {
backgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundBottom.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
selectedBackgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSelectedBottom.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
} else if (indexPath.row == 0 && count == 1) {
backgroundImage.frame = CGRectMake(10, -1, tableView.bounds.size.width-20, 45);
backgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSingle.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
selectedBackgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSelectedSingle.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
} else {
backgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundMiddle.png"] stretchableImageWithLeftCapWidth:1 topCapHeight:10];
selectedBackgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSelectedMiddle.png"] stretchableImageWithLeftCapWidth:1 topCapHeight:10];
}//end

backgroundImage.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[backgroundView addSubview:backgroundImage];
[backgroundImage release];

selectedBackgroundImage.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[selectedBackgroundView addSubview:selectedBackgroundImage];
[selectedBackgroundImage release];

cell.backgroundView = backgroundView;
[backgroundView release];

cell.selectedBackgroundView = selectedBackgroundView;
[selectedBackgroundView release];

linkAvailableImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.bounds.size.width-39, 9, 24, 24)] autorelease];
linkAvailableImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
linkAvailableImageView.image = [UIImage imageNamed:@"MDACLinkArrow.png"];
linkAvailableImageView.tag = 3;
[cell.contentView addSubview:linkAvailableImageView];

} else {
linkAvailableImageView = (UIImageView *)[cell.contentView viewWithTag:3];
}

// Get asset
cell.textLabel.opaque = NO;
cell.textLabel.text = asset.name;
cell.textLabel.font = [UIFont boldSystemFontOfSize:17];
cell.textLabel.backgroundColor = [UIColor colorWithWhite:94./255. alpha:1];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.6];
cell.textLabel.shadowOffset = CGSizeMake(0, -1);

// Set the kind of disclosure indicator
if ([asset.children intValue] > 0) {
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}//end

// Lazy Load the image
if (!asset.appIcon) {

// Download icon
[self startIconDownload:asset forIndexPath:indexPath];

// if a download is deferred or in progress, return a placeholder image
cell.imageView.image = [UIImage imageNamed:@"default-icon.png"];

} else {
cell.imageView.image = asset.appIcon;
}//end

return cell;

}//end

最佳答案

这里的问题是无论表格 View 中的位置如何,您都使用相同的单元格标识符。

因此,您最初根据 indexPath.row 和计数创建单元格,但是您将这些单元格与标识符 @"Cell"相关联。所以当你向下滚动时 dequeueReusableCellWithIdentifier将返回为列表开头配置的单元格 (indexPath.row == 0 && count > 1) 并将其用于列表结尾。

您需要确保单元格标识符反射(reflect)单元格开头的代码==nill if block ,以便您仅重用已为您正在创建的表中的位置配置的单元格。

作为Eiko指出,您还泄漏了 UIView 和 UIImageView 对象。您可以将它们放在 if block 中,显式释放它们或让它们自动释放。

关于iphone - UITableViewCell backgroundView 在不应该被重用时被重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6499901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com