ios - 单击自定义 uitableviewcell 中的一个 uibutton 会选择 uitableview 中的多个 uibutton

标签 ios objective-c uitableview uiscrollview

我在 uiviewcontroller 的 childViewController 中有一个 uitableview。在该 uitableview 中,我使用了自定义 uitableviewcell。在那个自定义的 uitableviewcell 中,我有最喜欢的复选框按钮。

此 uitableview 包含大约 30 条记录,大约 10 条记录出现在可见屏幕上,然后滚动另外 10 条,然后是另一条。

问题是当我在第一行选择一个 uibutton,然后向下滚动 uitableview 时,我看到它也在第 11 行和第 21 行被选中。我现在已经研究了半天,但无法发现问题。请提供任何帮助。

这是cellForRowAtIndexPath代码:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

ABCViewCell *cell = (ABCViewCell*)[tableView dequeueReusableCellWithIdentifier:@"ABCViewCell" forIndexPath:indexPath];
cell.delegate = self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.layoutMargins = UIEdgeInsetsZero;
cell.labelDesiredDate.text = [dateFormatter stringFromDate:[NSDate date]];
cell.labelJournaledDate.text = [dateFormatter stringFromDate:[NSDate date]];

return cell; }

谢谢, 阿卜杜勒

最佳答案

您必须记住,当您使用 dequeueReusableCellWithIdentifier 方法时,您的单元格开始重新绘制所有内容。因此,您必须考虑一种清理数据的方法。

内容如下:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ABCViewCell *cell = (ABCViewCell*)[tableView dequeueReusableCellWithIdentifier:@"ABCViewCell" forIndexPath:indexPath];
    if ([self shouldMyCellBeSelected]) {
        [cell displayButtonSelected];
    } else {
        [cell displayButtonUnselected];
    }
    return cell;
}
  1. shouldMyCellBeSelected。你应该有一个函数来确定特定的单元格是否在 应选择给定的位置。你可以通过拥有一个 用户单元格选择和比较的单独索引列表 当前待绘制单元格以查看它是否在该列表中。
  2. displayButtonSelected/displayButtonUnselected。然后你应该有一个功能来显示你的单元格选中和未选中。这可以通过更改按钮的图像或激活/停用它来实现。

希望对你有帮助

关于ios - 单击自定义 uitableviewcell 中的一个 uibutton 会选择 uitableview 中的多个 uibutton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27319804/

相关文章:

ios - 如何在 Swift/iOS 中将静态 TableView Controller 单元格限制到安全区域

ios - 将 JSON 解析为 TableView

iphone - iOS 中离线网站的最佳实践

ios - 无法在 iOS swift 中从 firebase 视频 url 生成视频缩略图?

ios - 如何将 IOS 照片库中的图像加载到 UIWebView?

iphone - Objective C IB 奥特莱斯

ios - 魔法记录和 iCloud 启用/禁用

ios - CocoapPods iOS Swift 中的 GoogleMaps SDK 错误

objective-c - Objective-C : Detecting Leakages via Instruments in Xcode

objective-c - UITableViewCell 底部信息文本