【问题标题】:Tableview Cell requires two taps to reveal the viewTableview Cell 需要点击两次才能显示视图
【发布时间】:2018-01-08 05:33:29
【问题描述】:

我在使用表格视图 didSelect 方法和 prepareForSegue 时遇到问题。我在我的应用中使用了 SWRevealController。选择单元格时,它会显示视图。 有时它无法正常工作。需要点按两次才能显示视图。几个月前,我使用了旧的显示视图框架,其中包含执行块操作。它工作得很好。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    for (int i=0; i<6; i++)
    {
        UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        if (i == indexPath.row)
        {
            cell.contentView.backgroundColor = [UIColor colorWithRed:KColorRedSelected green:KColorGreenSelected blue:KColorBlueSelected alpha:1];
        }
        else
        {
            cell.contentView.backgroundColor = [UIColor colorWithRed:KColorRed green:KColorGreen blue:KColorBlue alpha:1];
        }
    }
}

【问题讨论】:

  • 你能出示你的didSelect代码吗
  • 你为什么使用for (int i=0; i&lt;6; i++)
  • 更改单元格颜色。
  • 您是否在使用 tableview 的类中添加了任何手势或触摸事件方法?
  • @surjeet 不,我没有使用任何手势动作。

标签: ios objective-c swrevealviewcontroller


【解决方案1】:

将此代码添加到didSelectRowAtIndexPathdidDeselectRowAtIndexPath 函数中

    dispatch_async(dispatch_get_main_queue(), ^{

   //Write code what you need 

    });

这对我有用。

【讨论】:

    【解决方案2】:

    问题出在这一行

    [self tableView:tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];

    您正在调用 delegate 方法,该方法使可重用单元出列(我假设,因为它是标准行为)。您不想让可重复使用的单元格出列,您想对当前显示在indexPath 的单元格做一些事情。从UITableView做那个使用方法

    [tableView cellForRowAtIndexPath:indexPath];

    完整代码

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // grab the selected cell and give it selected color
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor colorWithRed:KColorRedSelected green:KColorGreenSelected blue:KColorBlueSelected alpha:1];
    }
    
    - (void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath 
    {
        // grab the deselected cell (if it's still visible) and give it deselected color
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor colorWithRed:KColorRed green:KColorGreen blue:KColorBlue alpha:1];
    }
    

    您还需要在UITableViewDelegate 方法cellForRowAtIndexPath 中设置适当的颜色,因为您设置的颜色在重复使用时会保留在单元格中。

    【讨论】:

    • 我编辑了答案,现在试试。你把事情复杂化了。您需要做的就是在didSelect 中抓取一个单元格并为其指定颜色,然后在didDeselect 中抓取一个单元格并为其取消选择颜色。
    • 另外,在您的UITableViewDelegate 方法cellForRowAtIndexPath 中,您还需要检查单元格是否被选中并为其赋予适当的颜色。
    • 我正在使用静态单元格。在我的场景中,我不需要调用 cellForRowAtIndexPath。这里所有的单元格也有segues。由于 segues didDeSelelctRowAtIndexPath 没有调用。
    【解决方案3】:

    这是获取点击单元格的完全错误的方法

    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
    

    也不要使用 for 循环。而不是这个你应该使用

    UITableViewCell *cell = [tableView cellForIndexPath:indexPath];
    

    获取正确的单元格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 2016-06-08
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多