【发布时间】:2016-07-15 06:38:32
【问题描述】:
我有 UITableViewCell 并在单元格左侧添加了按钮,用于选中或取消选中复选标记单元格。 uitableview 中的行数 50。
问题:
第一行被选中并将复选标记添加到按钮,然后我滚动 UITableView,我发现另一个复选标记被添加到另一个单元格。
是否有人面临同样的问题,我们将不胜感激。
//行索引路径的单元格
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:TableCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:UITableViewCell owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.checkButton.tag = indexPath.row;
// Storyboard 中的图像变化。
-(void)checkButtonAction:(id)sender
{
if ([sender isSelected]) {
[sender setSelected:NO];
}
else {
[sender setSelected:YES];
}
}
【问题讨论】:
-
查看Answer
-
UITableView 中的单元格是重用的,所以每次使用可能来自重用池的单元格时,您应该将其重置为默认状态。
-
您可以随意重置
tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { switch indexPath.section或tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)中的单元格!
标签: ios objective-c uitableview