Iphone UITableViewCell清除后仍显示与文本重叠的图像

Iphone UITableViewCell清除后仍显示与文本重叠的图像,iphone,cocoa-touch,uitableview,Iphone,Cocoa Touch,Uitableview,我有一个UITableViewController,它显示一组“n”行(所有行都使用标准的UITableViewCell类),其中最后一行有一个图像(一个小“+”图标指示“添加记录”) 假设以下场景: 用户单击最后一行,将新的视图控制器推送到导航堆栈上 然后,用户填写表格以添加新记录,该记录使用核心数据保存 然后从导航堆栈弹出表单,将您返回到第一个UITableViewController,我调用[self.tableView reloadData]以确保显示新状态 现在可以看到“n+1”行,最

我有一个
UITableViewController
,它显示一组“n”行(所有行都使用标准的
UITableViewCell
类),其中最后一行有一个图像(一个小“+”图标指示“添加记录”)

假设以下场景:

  • 用户单击最后一行,将新的视图控制器推送到导航堆栈上
  • 然后,用户填写表格以添加新记录,该记录使用核心数据保存
  • 然后从导航堆栈弹出表单,将您返回到第一个
    UITableViewController
    ,我调用
    [self.tableView reloadData]
    以确保显示新状态
  • 现在可以看到“n+1”行,最后一行现在显示“+”图像。问题是,我遇到了一个错误,行“n”仍然显示“+”图像,只是它与该单元格的文本标签重叠(我可以理解在文本左侧显示“+”图像的错误-为了避免这种情况,我明确设置了
    imageView.image=nil
    ,但图像与文本重叠的事实让这一个非常奇怪)

    以下是my
    cellForRowAtIndexPath
    函数的代码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *CellIdentifier = @"Cell";
        UITableViewCellStyle cellStyle;
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];        
        }
    
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
        if ((self.selectedCategory != nil) && (indexPath.row < [self.selectedCategory.subjects count])) {
            Subject *managedObject = (Subject*)[self.selectedCatSubjects objectAtIndex:indexPath.row];
            cell.textLabel.text = managedObject.name;
            cell.imageView.image = nil; // Needed in case there used to be a "+" icon
        } else {
            cell.textLabel.text = @"Add subject...";
            [cell.imageView initWithImage:_addIcon]; 
        }
    
        return cell;
    }
    
    -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
    {
    NSString*CellIdentifier=@“单元”;
    UITableViewCellStyle单元样式;
    UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    如果(单元格==nil){
    cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier]自动释放];
    }
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    if((self.selectedCategory!=nil)&&(indexath.row<[self.selectedCategory.subjects count])){
    Subject*managedObject=(Subject*)[self.selectedCatSubjects对象索引:indexPath.row];
    cell.textlab.text=managedObject.name;
    cell.imageView.image=nil;//如果以前有“+”图标,则需要
    }否则{
    cell.textlab.text=@“添加主题…”;
    [cell.imageView initWithImage:_addIcon];
    }
    返回单元;
    }
    
    尝试为最后一行使用不同的标识符:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *CellIdentifier = @"Cell";
        UITableViewCellStyle cellStyle;
    
        BOOL isFinalRow = (self.selectedCategory == nil) || (indexPath.row >= [self.selectedCategory.subjects count]);
        if (isFinalRow) {
            CellIdentifier = @"AddCell";
        }
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];        
        }
    
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
        if (!isFinalRow) {
            Subject *managedObject = (Subject*)[self.selectedCatSubjects objectAtIndex:indexPath.row];
            cell.textLabel.text = managedObject.name;
            cell.imageView.image = nil; // Needed in case there used to be a "+" icon
        } else {
            cell.textLabel.text = @"Add subject...";
            [cell.imageView initWithImage:_addIcon]; 
        }
    
        return cell;
    }
    

    尝试为最后一行使用不同的标识符:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *CellIdentifier = @"Cell";
        UITableViewCellStyle cellStyle;
    
        BOOL isFinalRow = (self.selectedCategory == nil) || (indexPath.row >= [self.selectedCategory.subjects count]);
        if (isFinalRow) {
            CellIdentifier = @"AddCell";
        }
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];        
        }
    
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
        if (!isFinalRow) {
            Subject *managedObject = (Subject*)[self.selectedCatSubjects objectAtIndex:indexPath.row];
            cell.textLabel.text = managedObject.name;
            cell.imageView.image = nil; // Needed in case there used to be a "+" icon
        } else {
            cell.textLabel.text = @"Add subject...";
            [cell.imageView initWithImage:_addIcon]; 
        }
    
        return cell;
    }
    

    我在我的一个应用程序中有一个类似的功能,我在最后一个单元格中有一个加载更多按钮。我在最后一个单元格中使用了不同的标识符,这对我来说很有效

    if(indexPath.row==[self.selectedCategory.subjects count]) 
    {
    cellIdentifier = @"PlusCell";
    }
    

    我在我的一个应用程序中有一个类似的功能,我在最后一个单元格中有一个加载更多按钮。我在最后一个单元格中使用了不同的标识符,这对我来说很有效

    if(indexPath.row==[self.selectedCategory.subjects count]) 
    {
    cellIdentifier = @"PlusCell";
    }
    

    这对我很有效。

    替换:

    static NSString *CellIdentifier = @"Cell";
    

    与:

    NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];
    

    这对我很有效。

    替换:

    static NSString *CellIdentifier = @"Cell";
    

    与:

    NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];
    


    我认为这与单元格的重用有关。能否尝试单独为最后一个单元格使用不同的单元格标识符。将NSString*CellIdentifier=@“cell”替换为NSString*CellIdentifier=[NSString stringWithFormat:@“cell%d%d”,indexPath.section,indexPath.row]@Krishnabhadra我认为这一点在一开始就有点违背了细胞再利用的意义,不是吗?@7KV7的方法听起来似乎更少overkill@andygeers当然这不是正确的方式这就是为什么我把它作为一个评论我只是想知道你在使用不同的单元格标识符后仍然有同样的问题坦白地说我不认为这是你的问题问题..@7KV7啊!!我明白了!我尝试了@Krishnabhadra使用节和行索引作为标识符的方法,但这没有帮助,因为即使单元格的使用发生变化,行索引也保持不变。如果我显式地将其更改为类似“PlusCell”的内容对于最后一行,它修复了错误。您能将其作为正确的答案发布,以便我可以投票接受吗?我认为这与单元格的重用有关。您能否尝试单独为最后一个单元格使用不同的单元格标识符。将NSString*CellIdentifier=@“cell”替换为NSString*CellIdentifier=[NSString stringWithFormat:@“单元格%d%d”,indexPath.section,indexPath.row]@Krishnabhadra我认为这一点在一开始就有点违背了细胞再利用的意义,不是吗?@7KV7的方法听起来似乎更少overkill@andygeers当然这不是正确的方式这就是为什么我把它作为一个评论我只是想知道你在使用不同的单元格标识符后仍然有同样的问题坦白地说我不认为这是你的问题问题..@7KV7啊!!我明白了!我尝试了@Krishnabhadra使用节和行索引作为标识符的方法,但这没有帮助,因为即使单元格的使用发生变化,行索引也保持不变。如果我显式地将其更改为类似“PlusCell”的内容"对于最后一行,它修复了错误。你能把它作为一个正确的答案发布,这样我就可以投票接受它吗?是不是有人建议内置图像视图有问题,因此你要实现自己的视图?@andygeers你有没有尝试过断点?当第n行被重用时,你是否进入了你的if条件。做一件事,try并隐藏imageview,而不是给出nil..cell.imageview.hidden=true;@Krishnabhadra我没有尝试过,但我想一定是这样,否则文本不会与图像重叠,它会像第n+1行一样缩进。@Krishnabhadra我只是尝试了
    hidden=true
    ,但没有帮助:(好主意!有人建议内置图像视图有问题,因此您正在实现自己的吗?@andygeers您是否尝试使用断点?当重用第n行时,您是否进入了if条件。做一件事,尝试隐藏图像视图,而不是给出nil..cell.imageview.hidden=true;@Krishnabhadra I haven没有试过,但我想一定是这样,否则文本不会与图像重叠,它会像第n+1行一样缩进。@Krishnabhadra我刚试过
    hidden=true
    ,但没有帮助:(好主意!这