【问题标题】:On scroll UITableViewCell added button image issue在滚动 UITableViewCell 添加按钮图像问题
【发布时间】: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.sectiontableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)中的单元格!

标签: ios objective-c uitableview


【解决方案1】:

阅读您的问题,它的单元格被 iOS 先生重复使用。下面这行是罪魁祸首。

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:TableCell];

它的作用是在表格视图中查找现有单元格,并加载该单元格。

你说,你点击了一个单元格,它有复选标记,对吧?

幸运的是,当您滚动离开它时,表格视图会通过上述调用重用该单元格,并在带有复选标记的可见单元格列表中再次看到。

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;

先生,在上述方法中查看您的呼叫时,您从未将复选标记设置为打开或关闭。您正在重复使用已经有复选标记的单元格。所以,当你再次看到它时,它也有复选标记,因为你从来没有改变过它。

注意:它不是随机给出复选标记。

在开始时,不要滚动。勾选所有单元格,您就会得到答案。现在开始滚动。现在每个单元格都应该有复选标记。

解决方案:

要解决此问题,请在您的数据模型中添加 isCheckmarkSelected:Bool 属性。 而且,每当用户检查单元格时,将 isCheckmarkSelected 设置为 true。

在方法调用中,[ cellForRowAtIndexPath ]...读取 isCheckmarkSelected 值并设置单元格的复选标记。

这将解决不那么随机的复选标记问题。

亲切的问候, 苏曼阿迪卡里

【讨论】:

    【解决方案2】:

    Click Here 上面的链接适用于我的问题。 在按钮操作中添加额外代码[[self.tableview] reloadData];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      相关资源
      最近更新 更多