我在 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/