Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios tableViewController不同单元格中的不同图像工作不正常_Ios_Uitableview - Fatal编程技术网

Ios tableViewController不同单元格中的不同图像工作不正常

Ios tableViewController不同单元格中的不同图像工作不正常,ios,uitableview,Ios,Uitableview,我制作了一个tableViewController。我想给第一个和第七个索引单元格提供不同的图像。这是我的cellforrowatinexpathdelegate方法中的代码片段。它在开始时正确初始化单元格,但当我上下滚动几次时,它也开始在其他单元格的附件视图中显示“button4.png” UIImage *indicatorImage; if(indexPath.row==0||indexPath.row==6)

我制作了一个
tableViewController
。我想给第一个和第七个索引单元格提供不同的图像。这是我的
cellforrowatinexpath
delegate方法中的代码片段。它在开始时正确初始化单元格,但当我上下滚动几次时,它也开始在其他单元格的附件视图中显示“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;

}

您正在创建和配置单元格的
if(cell==nil).
块中设置此图像。但是您需要将此图像设置移到
if
块之外,因为如果单元格被重用(即您不必
alloc
init
),旧图像将被重用。您应该查看
if
语句中的任何内容,以了解每个单元格都应该更改的内容,并将其移到
if
块之外。

当滚动将您弄得一团糟时,这表明您没有完全初始化重复使用的UITableViewCell的内容。虽然您的代码片段说您确实设置了它,但您的结果表明您没有设置。你可以找


例如,获取单元格后,立即尝试将accessoryView设置为nil,看看问题是否消失。

能否将整个cellForRowAtIndexPath方法张贴出来?我已在问题中添加了该函数的全部代码。请建议一些帮助。