如何在数据源方法中选择UITableViewCell

时间:2014-06-16 18:06:10

标签: ios uitableview

我正在填充UITableViewCell中的datasource方法,如果出现某些情况,我希望将其选定状态设置为ON

问题是UITableView没有跟踪单元格的状态(因为单元格还没有排队),因此这个所需的选择状态就丢失了

例如:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
   id someDataToPutInCell = [self.myData objectAtIndex:indexpath.row];
   if ([someDataToPutInCell satisfies some condition]) {
       cell.data = someDataToPutInCell;
       // now I've tried 2 different methods to select the cell
       // first method - directly set the cell to selected - doesn't work because the tableview deselects it when enqueuing it back to the table view
       [cell setSelected:YES];
        // second method - doesn't work because the cell doesn't have an idx path yet
       NSIndexPath *idx = [tableView indexPathForCell:cell];
        [tableView selectRowAtIndexPath:idx animated:NO scrollPosition:UITableViewScrollPositionNone];

   }
}

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题 -

在这个例子中,我试图通过API indexPathForCell找到单元格的索引路径 - 这是我正在做的,因为单元格的配置部分是在一个单独的方法中发生的(它没有访问权限)通过数据源方法的单元格的索引路径)我决定直接使用索引路径而不是尝试通过API搜索它,一切都解决了。

相关问题