我有一個非常簡單的UITableViewController
子類,用於向用戶顯示單元格中字母表中的字符。當用戶按下一個單元格時,將其附件類型設置爲複選標記。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
表視圖工作正常。我在我的原型單元格的故事板中設置了我的reuseIdentifier屬性,並且在我開始選擇單元格之前它看起來都很好。
問題:當我選擇任何單元格時,比如說「A」單元格,當我向下滾動它們時,其他尚未可見的單元格也會給出複選標記。更糟糕的是,當我上下滾動時,有時單元格「A」上的複選標記將被刪除並提供給單元格「B」。
@downvoter爲什麼呢? – michaelsnowden