【问题标题】:selecting a custom cell repeats in table在表格中选择自定义单元格重复
【发布时间】:2014-09-17 11:36:20
【问题描述】:

我正在使用自定义单元格,其中有一个按钮,单击单元格时应将其标记为选中。但是当我单击一个单元格时,它会在表格中重复,例如。

单元格 1

单元格 2

单元格 3

单元格 4

单元格 5

单元格 6

单元格 7(从 1 开始重复检查)

单元格 8(从 2 处重复检查)

这是我的代码。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    WIWNotificationViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WIWNotificationViewCell" forIndexPath:indexPath];
    cell.notifySelected=NO;
    if (cell == nil)
    {
        cell = [[WIWNotificationViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WIWNotificationViewCell"];
    }

    return cell;
}




    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WIWNotificationViewCell *cell = (WIWNotificationViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (cell.notifySelected==YES)
    {
        [cell.btnNotifyClicked setSelected:NO];
        cell.notifySelected=NO;
    } else
    {
        [cell.btnNotifyClicked setSelected:YES];
        cell.notifySelected=YES;
    }
}

WIWNotificationViewCell 是一个自定义单元格,它包含要检查的 btnNotifiedClicked。

【问题讨论】:

  • 您需要为它维护数据源,例如保留一个数组来跟踪它
  • 怎么样?你能解释一下吗??
  • WIWNotificationViewCell 的地址代码 - 您与 btnNotifyClicked 的联系方式
  • 我只在自定义单元格中为 btnNotifyClicked 提供了一个出口。

标签: ios uitableview custom-cell


【解决方案1】:

当您使用重用标识符获取单元格时,您所做的只是设置布尔值 notifySelected。您还需要调用

[cell.btnNotifyClicked setSelected:NO];

在 cellForRowAtIndexPath 中,否则视图的状态直到 didSelectRowAtIndexPath 才会更新

【讨论】:

  • 最好在prepareForReuse做这个
猜你喜欢
  • 2012-07-10
  • 1970-01-01
  • 1970-01-01
  • 2017-06-25
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
相关资源
最近更新 更多