我做了tableViewController
。我想给第1和第7个索引单元提供不同的图像。这是我的cellForRowAtIndexPath
委托方法中的代码段。它在开始时正确初始化单元格,但当我上下滚动几次时,它也开始在其他单元格的附件视图中给出“button4.png”。
UIImage *indicatorImage;
if(indexPath.row==0||indexPath.row==6)
{
indicatorImage = [UIImage imageNamed:@"button4.png"];
}
else
{
indicatorImage = [UIImage imageNamed:@"img-arrow.png"];
}
cell.accessoryView =
[[UIImageView alloc]
initWithImage:indicatorImage];
可能的原因是什么?
该功能的完整代码有点乱,但我在这里发布:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
selection=self.tabBarController.selectedIndex;
static NSString *CellIdentifier = @"Cell";
UILabel *topLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIImage *indicatorImage;
if(indexPath.row==0||indexPath.row==6)
{
indicatorImage = [UIImage imageNamed:@"button4.png"];
}
else
{
indicatorImage = [UIImage imageNamed:@"img-arrow.png"];
}
cell.accessoryView =
[[UIImageView alloc]
initWithImage:indicatorImage];
cell.accessoryView =
[[UIImageView alloc]
initWithImage:indicatorImage];
const CGFloat LABEL_HEIGHT = 20;
topLabel =
[[UILabel alloc]
initWithFrame:
CGRectMake(
2.0 * cell.indentationWidth,
0.5 * (tableView.rowHeight - 2 * LABEL_HEIGHT),
tableView.bounds.size.width -
4.0 * cell.indentationWidth
- indicatorImage.size.width,
LABEL_HEIGHT)];
[cell.contentView addSubview:topLabel];
topLabel.backgroundColor = [UIColor clearColor];
topLabel.shadowColor = [UIColor whiteColor];
topLabel.shadowOffset = CGSizeMake(0, 1);
topLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
topLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
topLabel.tag=10;
cell.backgroundView =
[[UIImageView alloc] init];
cell.selectedBackgroundView =
[[UIImageView alloc] init];
}
}
else
{
topLabel = (UILabel *)[cell viewWithTag:10];
}
UIImage *rowBackground;
UIImage *selectionBackground;
rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
if(indexPath.row==0||indexPath.row==6)
{
rowBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
topLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
topLabel.font = [UIFont systemFontOfSize:20];
}
((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
NSString *object = _objects[indexPath.row];
NSString *str=[object description];
topLabel.text=[NSString stringWithFormat:@" %@",str];
return cell;
}
答案 0 :(得分:2)
您正在创建和配置单元格的if (cell == nil) ...
块中设置此图像。但是您需要将此图像设置移到该if
块之外,因为如果正在重复使用该单元格(即您不必alloc
和init
),则旧图像将是重复使用。您应该检查if
语句中的任何内容,以了解应逐个更改的内容,并将其移到if
块之外。
答案 1 :(得分:0)
当你像这样滚动混乱时,它表明你没有完全初始化 REUSED UITableViewCell的内容。虽然您的代码段说您确实将其设置为某些内容 - 但您的结果表明您并非如此。你可以找那个。
例如,尝试在获取单元格后立即将accessoryView设置为nil,然后查看问题是否消失。