Что происходит, так это то, что UITableView перерабатывает UITableViewCells для экономии памяти. Это означает, что когда вы прокручиваете список вниз, UITableView снимает ячейки с верхней части таблицы и использует их для отображения более поздних элементов, поэтому при прокрутке вверх они потеряли состояние.
Вы можете исправить это, сохранив NSMutableSet
проверенных indexPaths. Когда пользователь проверяет элемент, вы добавляете его indexPath
к этому набору. Затем в вашем cellForRowAtIndexPath
вы можете проверить, есть ли этот элемент в вашем наборе отмеченных элементов.
UPDATE
Вот пример того, как это может работать:
# MyTableView.h
@interface MyTableView: UITableView
<UITableViewDataSource, UITableViewDelegate>
{
NSMutableSet *checkedIndexPaths;
}
@property (nonatomic, retain) NSMutableSet *checkedIndexPaths;
@end
тогда
# MyTableView.m
#import "MyTableView.h"
@implementation MyTableView
@synthesize checkedIndexPaths;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Normal layout stuff goes here...
// ***Add code to make sure the checkbox in this cell is unticked.***
for (NSIndexPath *path in self.checkedIndexPaths)
{
if (path.section == indexPath.section && path.row == indexPath.row)
{
// ***We found a matching index path in our set of checked index paths, so we need to show this to the user by putting a tick in the check box, for instance***
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Normal stuff to handle visual checking/unchecking of row here
// Lazy-load the mutable set
if (!self.checkedIndexPaths)
checkedIndexPaths = [[NSMutableSet alloc] init];
// If we are checking this cell, we do
[self.checkedIndexPaths addObject:indexPath];
// If we are unchecking, just enumerate over the items in checkedIndexPaths and remove the one where the row and section match.
}
@end
Это всего лишь скелетный код, который не тестировался, но, надеюсь, даст вам шанс.