【问题标题】:UITableViewCell setSelected:animated: not workingUITableViewCell setSelected:动画:不工作
【发布时间】:2014-03-27 20:28:39
【问题描述】:

这快把我逼疯了。我在 S.O. 上看过这里。因为我认为是一个简单的答案,但找不到。

在我的自定义UITableViewCell

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{   
    // Configure the view for the selected state
    if (selected) {        
        [self.languageLevelNameLabel setTextColor:[UIColor blackColor]];        
    }
    else {        
        [self.languageLevelNameLabel setTextColor:[UIColor colorMessageCountZero]];
    }

    [self setNeedsDisplay];
}

在控制器的tableView:cellForRowAtIndexPath:中:

 if ([level integerValue] == indexPath.row) {

        [cell setSelected:YES];
    }

我已插入断点,selected == YES 被传递给正确的单元格,if 语句在应该执行的时候被执行,但文本永远不会设置为 blackColor

【问题讨论】:

  • UITableViewCell 的 -setSelected: 不会在内部调用 -setSelected:animated:NO

标签: objective-c cocoa-touch uitableview


【解决方案1】:

要使单元格显示为选中状态,您必须从 -tableView:willDisplayCell:forRowAtIndexPath: 中调用 -setSelected:animated:,如下所示:

目标 C:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (/* should be selected */) {
        [cell setSelected:YES animated:NO];
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];  // required, as noted below
    }
}

斯威夫特 3:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if (/* should be selected */) {
        cell.setSelected(true, animated: false)
        tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)  // required, as noted below
    }
}

从其他任何地方调用-setSelected 无效。

为什么? 在初始化或出列单元格之后,但在显示它之前,表格视图会调用一个名为 -_configureCellForDisplay:forIndexPath: 的私有方法,除其他外,它将单元格的 selected 属性设置为 NO。代理的willDisplayCell:forRowAtIndexPath: 在此之后被调用,让您设置显示所需的任何内容。

【讨论】:

  • 看到这个...知道为什么会这样吗?
  • 关于 tableview 单元格如何工作的重要信息!
  • 也很重要,我们还需要调用tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: .Top) 否则该单元格将变得不可选择(委托既不响应tableView:didDeselectRowAtIndexPath 也不响应tableView:didSelectRowAtIndexPath
  • 我想知道你们俩是怎么想出来的。我希望 Apple 文档有这些信息.. 1)由于出队而在 willDisplayCell 内调用 setSelected 和 2)调用 selectRowAtIndexPath。做得好。我编写了自己的选择代码,因为我无法将行默认为选中。
  • 据我所知,您不需要同时调用 cell.setSelected 和 tableView.selectRow。您只需要调用 tableView.selectRow 即可正常工作。
【解决方案2】:

如果您想将单元格设置为选中,请在表格视图的 cellForRowAtIndexPath 方法中使用 selectRowAtIndexPath:animated:scrollPosition: 方法

目标-C

[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

斯威夫特 4

tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)

【讨论】:

  • viewWillAppear调用这个方法为我做了。在viewDidLoad 中调用它没有效果,在cellForRowAtIndexPath 中调用它对我来说似乎有点过头了(swift 5 / 2020)。
【解决方案3】:

试试

NSIndexPath* selectedCellIndexPath= [NSIndexPath indexPathForRow:number_row  inSection:0];
[self tableView:tableViewList didSelectRowAtIndexPath:selectedCellIndexPath];
[tableViewList selectRowAtIndexPath:selectedCellIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

【讨论】:

    【解决方案4】:

    不需要调用cell的setSelected方法。Apple希望我们通过两个函数来设置cell的选中状态,一个是didSelectRowAtIndexPath,另一个是didDeselectRowAtIndexPath,它们总是成对出现。为了解决这个问题,我们只需要调用两个函数:

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    
    [self tableView:self.leftTableView didSelectRowAtIndexPath:indexPath];
    [self.leftTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      相关资源
      最近更新 更多