8

我经常需要将表格视图单元格设置为使用以下代码的初始选定状态:

[self.tableView selectRowAtIndexPath:indexPath 
   animated:NO scrollPosition:UITableViewScrollPositionNone];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell setSelected:YES];
cell.accessoryType = UITableViewCellAccessoryCheckmark;

我同时使用selectRowAtIndexPath:indexPathand setSelected:YES,因为我不完全理解这两种方式中的哪一种是以编程方式选择单元格的首选方式。

我应该使用哪一种陈述,为什么?

4

2 回答 2

8

我相信您要使用的方法是selectRowAtIndexPath:animated:scrollPosition:. 通常,您应该将单元状态的管理留给表格视图。在选择的情况下,它存储并维护一组选定的索引路径,因此在为它重用不同的单元格后,正确的行将保持选中状态。也不需要调用这两种方法,它只是多余的。

于 2013-02-17T11:51:38.940 回答
0

就我而言

let indexPath = IndexPath(item: viewModel.item, section: viewModel.section)

self.categoryCollectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionView.ScrollPosition.centeredHorizontally)

if let cell = self.categoryCollectionView.dequeueReusableCell(withReuseIdentifier: SubcategoryCollectionViewCell.identifier, for: indexPath) as? SubcategoryCollectionViewCell {
    //...//
}
于 2020-07-28T20:23:01.680 回答