我正在开发一个具有UITableView
和定制UITableViewCells
的应用程序。单元格包含复选框和3个标签。用户可以通过点击复选框来选择多行。当我选择多行并滚动表格视图时,选中标记从复选框中消失。这意味着所选行被更改为未选模式。这可以如何解决?在UITableView中滚动定制单元格
0
A
回答
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:在单元格回滚到视图时被调用。复选框默认为未选模式。将复选框的状态存储在一个数组中,并将单元格的复选框状态设置为
相关问题
- 1. UITableView的自定义单元格滚动
- 2. tvOS UITableView滚动到单元格
- 3. UITableView添加新单元格时滚动
- 4. 设置滚动最后UItableview单元格
- 5. 单元格图像更改滚动UITableView
- 6. 滚动时UITableView单元格变空白
- 7. UITableView滚动非常大的单元格
- 8. 重复单元格,同时滚动uitableview
- 9. 定制UITableView滚动!
- 10. UITableView滚动时为单元格制作动画
- 11. 使用固定的“Header”在UITableView上滚动单元格
- 12. 无法保存滚动,因此UITableview中的UITableView单元格
- 13. iOS在UITableView中定制滚动条
- 14. 在滚动之前加载UITableView中的所有单元格
- 15. 在Uitableview单元格中复制?
- 16. 几个UITableView的自定义单元格。不工作滚动
- 17. 使用自定义单元格滚动UITableView时提高性能
- 18. UITableView滚动到导航后选定的单元格
- 19. UITableView自定义单元格,滚动和内存管理
- 20. 滚动时自定义单元格重新加载问题UITableView
- 21. UITableView作为另一个UITableView上的单元格取消滚动
- 22. UITableView单元格标签在滚动时左右移动
- 23. 在选定的单元格中的UITableView中的部分之间滚动
- 24. UITableview单元格动画
- 25. 自定义uitableview单元格?
- 26. 在包含每个单元格中的UI元素的UITableView中慢速滚动
- 27. 滚动UITableView单元格超出表格界限
- 28. UITableview基于滚动添加表格单元格
- 29. UITableView单元格在滚动时分组搞乱了
- 30. 防止在移除最后一个单元格后滚动uitableview
请发表您的代码为tableView:cellForRowAtIndexPath: – Denis