iphone - UITableViewController 按单元格标识符编辑

标签 iphone ios uitableview edit

acaniChat 的 ChatViewController 有两种不同类型的带有单元格标识符的单元格:@"MessageCellID"@"TimestampCellID"。我该如何做到只有消息单元格是可编辑的?

这就是我所做的。它有点管用,但只有屏幕上的消息单元格是可编辑的。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return [[chatContent cellForRowAtIndexPath:indexPath] reuseIdentifier] == MessageCellId;
}

这适用于所有细胞,但这是正确的做法吗?

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return [[cellMap objectAtIndex:[indexPath row]] isKindOfClass:[Message class]];
}

最佳答案

我的“答案”更多是建议您使用数据源检查而不是单元格检查来确定单元格是否可编辑。 tableView:canEditRowAtIndexPath: 是一个 UITableViewDataSource 协议(protocol)消息。它是数据源一部分的原因是 b/c 通常它是数据模型逻辑,它确定特定单元格中的数据是否应该是可编辑的。在您的两个示例中,您都使用单元格类型作为单元格中数据类型的代理。

更好的检查类似于您必须在 tableView:cellForRowAtIndexPath: 中使用的逻辑,以确定要使用哪种类型的单元格。

在不知道数据模型的细节的情况下,我不能给你确切的代码,但它是这样的:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self chatDataTypeAtIndexPath:indexPath] == ChatDataTypeMessage;
}

关于iphone - UITableViewController 按单元格标识符编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5608251/

相关文章:

iphone - 将 subview 添加到 map 上方但注释 View 下方的 MKMapView?

iPhone 检查空字典

ios - 使用 AFNetworking NSOperations 连续下载大量文件......内存不足

ios - 可变长度标签、tableview 行高和自动布局

ios - 如何使用 Swift 以编程方式创建多个 UITableViews?

iphone - 隐藏 UITableView 搜索栏

android - 是否可以获取已安装应用程序的用户名和密码?

ios - Xcode 自动建议和索引问题

ios - UITabBarVC 内的 UISplitViewController : showDetail and hide tab bar

ios - 归档因 XCode 10、Cocoapods 和新构建系统而失败