我有一个非常简单的UITableViewController
子类,旨在向用户显示单元格中字母表中的字符。当用户按下一个单元格时,它会将其附件类型设置为复选标记。
#import "MTGTableViewController.h"
@interface MTGTableViewController ()
@property (nonatomic, strong) NSArray *data;
@end
@implementation MTGTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_data = @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = _data[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
@end
表格视图工作正常。我在原型单元格的情节提要中设置了我的重用标识符属性,在我开始选择单元格之前,一切看起来都很好。
问题:当我选择任何单元格时,比如“A”单元格,当我向下滚动到其他不可见的单元格时,它们也会被选中。更糟糕的是,当我上下滚动时,有时会删除单元格“A”上的复选标记并赋予单元格“B”。