2015-09-26 31 views
0

我制作了一个大型的UITableView和27个静态UITableViewCell s。当选择一个UITableViewCell应该设置其accessoryTypeUITableViewAccessoryCheckmark,并最后选定UITableViewCellaccessoryTypeUITableViewAccessoryNoneUITableViewCell无法在大型UITableView中看到(Xcode,iOS)

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    // get an NSInteger from NSUserDefaults 

    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:savedInteger inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop]; 
    [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:savedInteger inSection:0]]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // get the NSInteger again 

    UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:savedInteger inSection:0]]; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    lastCell.accessoryType = UITableViewAccessoryNone; 
    cell.accessoryType = UITableViewAccessoryCheckmark; 

    // save indexPath.row in NSUserDefaults and do some other stuff 

,当你选择一个UITableViewCell是接近最后选定UITableViewCell但是当最后选定的这工作得很好UITableViewCell大约有11行左右,UITableViewCell *lastCellnil并且复选标记不会消失。

为什么它的行为如何,我该如何解决它?提前致谢。因为UITableView的重用细胞(当您从顶部滚动细胞它初始化只有少数细胞进入底部等,它是有效的)

看到类似的问题

+0

不显式调用'didSelectRowAtIndexPath'。 – rmaddy

+0

您应该在'cellForRowAtIndexPath'中设置单元的accessoryType。 – rmaddy

+0

在你的模型对象中有1个布尔,并且从那个布尔你可以处理accessoryView。 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath'但是当我尝试使用UITableViewCell获取单元格时,如果bool是真的然后显示它,否则不要.. –

回答

0

的cellForRowAtIndexPath返回唯一可见的细胞here

0

cellForRowAtIndexPath返回如果单元格不可见或索引路径超出范围。
因此,检查当前选中的单元格是否可见。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    // get the NSInteger from NSUserDefaults 
    NSIndexPath *previousSelection = [NSIndexPath indexPathForRow:savedInteger inSection:0]; 
    if ([tableView.indexPathsForVisibleRows containsObject:previousSelection]) { 
     UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:previousSelection]; 
     lastCell.accessoryType = UITableViewAccessoryNone; 
    } 

    // save indexPath.row in NSUserDefaults 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.accessoryType = UITableViewAccessoryCheckmark; 

    // do other stuff 
} 

另外,为了确保表视图细胞与在第一位置的所需accessoryType创建的,在的cellForRowAtIndexPath设置表视图小区的accessoryType

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

    // get the table view cell and set the details 

    cell.accessoryType = UITableViewAccessoryNone; 

    // get the NSInteger from NSUserDefaults 
    if (indexPath.row == savedInteger) { 
     cell.accessoryType = UITableViewAccessoryCheckmark; 
    } 

    return cell; 
} 
+0

* cell = [tableView cellForRowAtIndexPath:indexPath]'UITableViewCell * cell'为'nil'。当我尝试使用单元标识符来获取'UITableViewCell'时,控制台告诉我必须使用原型单元。 –

相关问题