ios - 当我尝试只检查 tableview 中的一个单元格时出现错误

标签 ios objective-c uitableview checkmark

当我在加载后检查第一个单元格时——没有任何反应,我一遍又一遍地点击——没有任何反应。我可以检查其他单元格、第二个、第三个等,然后我才能检查第一个单元格。这是我的方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = indexPath.row;
    NSUInteger oldRow = lastIndexPath.row;
    if (oldRow != row) {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        lastIndexPath = indexPath;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

或者也许你可以建议其他方式来制作它(只检查表格 View 中的一个单元格),因为我发现只有包含大量代码且难以理解的模型。

最佳答案

因为一开始你的lastIndexPath变量是nil,所以lastIndexPath.row会返回0。如果你点击第一行,那一行也是0,所以不会进入if语句。将该语句替换为:if (!lastIndexPath || oldRow != row)

关于ios - 当我尝试只检查 tableview 中的一个单元格时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19929882/

相关文章:

iphone - UITableView 单元格在 6 之后重复,但在单击时显示正确的数据

objective-c - 从 iOS 上的 UIView 将图像保存到应用程序文档文件夹

iphone - 将 UIWebView 适配到 UITableViewCell

objective-c - 仪器中出现奇怪的内存泄漏

swift - UIViewController 内的 UITableView

ios - 改变图像色调 iOS

iphone - 从网络读取 .txt 文件 - Objective-C

ios - 为 iOS 和 watchOS 创建框架目标

ios - 如何以编程方式将 UIImageView 缩放到固定宽度和灵活高度?

ios - 自定义 UITableViewCell 中 UIView 的仅两个角 - iOS