2013-05-12 41 views
0

我选择编程方式使用细胞:无法选择或访问的UITableView细胞即出滚动界限(不可见的细胞)的

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:1]; 
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 

UITableViewCell *anotherCell = [self.tableView cellForRowAtIndexPath:indexPath]; 
[anotherCell setSelected:YES]; 
anotherCell.accessoryType = UITableViewCellAccessoryCheckmark; 

这个伟大的工程在UITableView中的所有其他细胞,但对于那些超越视图边界的夫妇,当我向下滚动时,我看到单元格上没有复选标记。

如何将复选标记/选中单元格通过滚动条?

+0

如果不使用这个,你可以保存在一个数组左右,在'的cellForRowAtIndexPath这个选择indexPath的:'用这个信息来显示复选标记。将dataSource绑定到视图然后重新加载视图总是更容易。 – Anupdas 2013-05-12 21:46:11

+0

请记住,不可见的单元通常不存在,并根据需要创建。因此你不能(也不应该)使用你的视图来存储数据。这就是你的模型的用途。将复选标记的状态保存在模型或本地属性中,并将其设置为cellForRowAtIndexPath。 – lnafziger 2013-05-12 22:03:10

+0

thx!即时通讯确实没有存储在单元格中的数据,但我需要保持表的状态,当它再次加载。当表加载时,我应该在哪里按程序选择单元格? – stackOverFlew 2013-05-12 22:34:58

回答

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

    static NSString *CellIdentifier = @"genericCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    cell.textLabel.font = [UIFont systemFontOfSize:22.0]; 


    //set selected based on indexPath.row and indexPath.section 
    [cell setSelected:YES]; 


    //or 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 



}