我做了一个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;
}