2011-11-29 85 views
0

我正在开发一个具有UITableView和定制UITableViewCells的应用程序。单元格包含复选框和3个标签。用户可以通过点击复选框来选择多行。当我选择多行并滚动表格视图时,选中标记从复选框中消失。这意味着所选行被更改为未选模式。这可以如何解决?在UITableView中滚动定制单元格

+0

请发表您的代码为tableView:cellForRowAtIndexPath: – Denis

回答

1

我认为您的问题可能与单元重用有关。创建你的控制器的NSMutableDictionary(应该是和实例变量),并且像这样创建:

dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys: 
       [NSNumber numberWithBool:NO], [NSIndexPath indexPathForRow:0 inSection:0], 
       [NSNumber numberWithBool:NO], [NSIndexPath indexPathForRow:1 inSection:0], nil]; 

这样做对所有的行,或者如果需要的话,计算行及其索引,但设置它们的所有对象到[NSNumber numberWithBool:NO

现在,tableView:didSelectRowAtIndexPath:

if ([(NSNumber *)[dictionary objectForKey:indexPath] isEqualToNumber:[NSNumber numberWithBool:NO]) { 
    [dictionary setObject:[NSNumber numberWithBool:YES] forKey:indexPath]; 
    // Do any visual updating necessary 
} else { 
    [dictionary setObject:[NSNumber numberWithBool:NO] forKey:indexPath]; 
    // Do any visual updating necessary 
} 

现在,我们有存储单元是否被选中与否的一种方式,改变你的小区标识,为您的电池工作:

NSString *identifier = [[dictionary objectForKey:indexPath] stringValue]; 
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease]; 
} 

// Do any visual updating necessary 
return cell; 

现在,更新您的自定义单元类以接受@"0"@"1"作为标识符,并根据此输入自动更新其选定状态。

0

我猜测这是因为细胞正在被重新使用。这意味着cellForRowAtIndex:在单元格回滚到视图时被调用。复选框默认为未选模式。将复选框的状态存储在一个数组中,并将单元格的复选框状态设置为

相关问题