【问题标题】:Wrong Editing Controls Displayed in UITableViewUITableView 中显示错误的编辑控件
【发布时间】:2010-05-25 16:36:10
【问题描述】:

我在使用 UITableView 时遇到了一个奇怪的问题。当用户点击编辑按钮时,tableview(这是一个包含多个部分的分组视图)应该显示每一行的删除按钮——除了每个部分的最后一行,它有一个绿色的添加按钮。

当用户点击绿色按钮时,会插入一个新行,但现在最后一行会有一个删除按钮。更奇怪的是,删除按钮就像添加按钮一样。因此,似乎存在绘图故障,而不是分配正确样式的问题。 (广泛的 NSLogging 显示最后一个单元格正在正确获取插入编辑样式。)

我尝试在单元格和 tableView 上设置 setNeedsDisplay,我尝试重新加载该部分/行/整个表,但问题仍然存在。关于如何让 UITableView 显式重绘编辑控件的任何想法?

【问题讨论】:

  • 这可能与您的 tableview 单元格被重用有关。您是否尝试过将该特定单元格显式设置为您想要的样式?您可能希望发布一些“添加”按钮单元格操作的代码,以便人们更好地了解该方法对 tableview 的作用。

标签: iphone uitableview editing


【解决方案1】:

我遇到了同样的问题。看来细胞被重复使用太多了..

您可以通过使用不同的标识符为插入的单元格强制执行新单元格来修复它:

if([tableView isEditing] && indexPath.row == [items count]){
    static NSString *CellIdentifier = @"AddCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // "add-cell" setup here.. e.g"
    [[cell textLabel] setText:@"Tab to add new item"];

    return cell;
}else{
    static NSString *CellIdentifier = @"NormalCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // normal cell setup here..

    return cell;
}

【讨论】:

    【解决方案2】:

    我很确定你可以在你的 UITableView 委托中实现它来防止它编辑最后一行:

    
    -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
      // Return NO if you do not want the specified item to be editable.
      if (indexPath.row != lastRow) // whatever your last row is
        return YES;
      }
      return NO;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-02
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多