ios - UITableView中的UIButton点击 objective-c 中的多个按钮

标签 ios objective-c uitableview

我在 UITableview 中有 2 个按钮。当我在我的 UITableView 中选择一个按钮时,其他下方的按钮也会被单击。为什么单击一个按钮会选择多个 UIButton?

我正在使用 Storyboard,我的代码是:

- (IBAction)yesBtnAction:(id)sender {
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *indexPath = [self.take5table indexPathForCell:clickedCell];
    UIButton *yesBTN=(UIButton *)[clickedCell.contentView viewWithTag:100];
    UIButton *noBTN=(UIButton *)[clickedCell.contentView viewWithTag:111];
    [yesBTN setBackgroundColor:[UIColor colorWithRed:0.23 green:0.62 blue:0.23 alpha:1.0]];
    [noBTN setBackgroundColor:[UIColor whiteColor]];
}

- (IBAction)noBtnAction:(id)sender {
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *indexPath = [self.take5table indexPathForCell:clickedCell];
    UIButton *yesBTN=(UIButton *)[clickedCell.contentView viewWithTag:100];
    UIButton *noBTN=(UIButton *)[clickedCell.contentView viewWithTag:111];
    [yesBTN setBackgroundColor:[UIColor whiteColor]];
    [noBTN setBackgroundColor:[UIColor redColor]];
}

如有任何帮助/建议,我们将不胜感激。

最佳答案

问题是您没有正确处理单元重用。

您需要确保您的 cellForRowAtIndexPath 正确设置了每个按钮的状态。这需要基于您在数据模型中跟踪的某些状态。此数据模型需要在点击每个按钮时更新。

您正试图保持按钮中的状态。那行不通。

关于ios - UITableView中的UIButton点击 objective-c 中的多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40458434/

相关文章:

iphone - 如何在自定义单元格中使用 autoresizingMask 制作 UILabel?

iphone - 将 iOS 应用程序连接到核心数据

ios - 当应用程序暂停时,我如何知道 NSURLSessionUploadTask 是否正常工作?

ios - 列表项的文本在一定长度后被截断

ios - 如何使用 NSDateFormatter 将字符串日期转换为 NSDate

ios - Collection View 在设备上显示的结果与在模拟器上不同

ios - 如何在关闭 ViewController 时将数据传递给另一个 Controller ?

ios - Facebook SDK,是否可以通过用户名获取所有用户?

ios - 从嵌套在 TableViewCell 中的 CollectionViewCell 中呈现 ViewController

ios - 无法在 Swift 中的 TableView 的另一个 View Controller 中显示值