【问题标题】:IOS Tableview select row not workingIOS Tableview选择行不起作用
【发布时间】:2016-04-29 04:54:32
【问题描述】:

我正在尝试将复选标记添加到选定行并在再次选择时将其删除,但每次我选择一行时,复选标记只会转到第一个单元格并在此之后向下移动(即我单击任何单元格,然后单元格 0 有一个复选标记,我单击任何其他单元格,单元格 1 有一个复选标记,等等...)。

这是我的 didselectrowatindexpath 和 cellforrowatindexpath 方法:

更新:

// the cell will be returned to the tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"preflabel"forIndexPath:indexPath];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"preflabel"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%@", data[indexPath.row]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"preflabel"forIndexPath:indexPath];

    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}

【问题讨论】:

  • 可能是因为reloadData?重新加载表格视图会清除当前状态,包括当前选择。但是,如果您显式调用 reloadData,它会清除此状态,并且任何后续对 layoutSubviews 的直接或间接调用都不会触发重新加载。
  • 不要将您的代码作为图片发布。使用实际代码更新您的问题(并请正确格式化)。
  • didSelectRowAtIndexPath中删除[tableView reloadData]
  • 我尝试删除重新加载数据方法,现在所发生的只是行突出显示一秒钟,然后取消突出显示本身。我还尝试删除 deselectrowatindextpath 调用,但现在发生的情况是该框已选中并且也变灰,并且文本不再可见。当我这样做时,我无法取消选择表格框(这也应该是可能的)
  • @rmaddy 我认为您的意思是说“您不应该将代码作为图像发布,这会给版主带来问题。我们可以选择添加您应该使用的代码。”这样一来,您的攻击性就会降低,并且您会让我想将其更改为代码,而不是想再上传 10 张代码图像来打扰您。

标签: ios objective-c uitableview


【解决方案1】:

你有 [tableView reloadData] 这会清除你的 tableview 的当前状态。

重新加载表格视图会清除当前状态,包括当前选择。但是,如果您显式调用 reloadData,它会清除此状态,并且任何后续对 layoutSubviews 的直接或间接调用都不会触发重新加载。

更多信息see.

更新

您正在 didSelectRowAtIndexPath 和 cellForRowAtIndexPath 中创建 tableviewcells。你为什么做这个?这将覆盖被选中的单元格。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
 cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

【讨论】:

  • 删除它仍然不会为所选行添加复选标记。该行仅突出显示一秒钟然后取消突出显示,这就是它的结尾,没有复选标记
  • 没问题!很高兴你把它修好了。
【解决方案2】:

你不应该在select方法中通过deque方法获取单元格,因为你得到的单元格不是你触摸的单元格,像deselect方法一样使用cellForCellAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryNone;
}

【讨论】:

    猜你喜欢
    • 2014-07-07
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多