因此,我的UITableView中有很多單元格,並且在觸摸單元格時,剛剛出現在視圖外的其他單元格也應用了刻度。我相信這是因爲細胞被再利用或因dequeueReusableCellWithIdentifier:
在單個UITableCell觸摸上應用了多個UITableViewCellAccessoryCheckmark問題
繼承人我的代碼的東西:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone){
[addPapersSelectedRowsArray addObject:[NSNumber numberWithInt:indexPath.row]];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else if (cell.accessoryType == UITableViewCellAccessoryCheckmark){
cell.accessoryType = UITableViewCellAccessoryNone;
[addPapersSelectedRowsArray removeObject:[NSNumber numberWithInt:indexPath.row]];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *currentPapersSecltion = [papersDataDictionary objectAtIndex:indexPath.row];
[[cell textLabel] setText:[currentPapersSecltion objectForKey:@"Title"]];
return cell;
}
也從一個在的.plist拉viewDidLoad
數據:
//Load papers data from the local plist
papersDataDictionary = [[[NSDictionary alloc] initWithContentsOfFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"papers.plist"]] objectForKey:@"Rows"];
有誰知道如何解決這個問題? 謝謝你們!
敏捷
您的cellForRowAtIndexPath數據源方法的郵政編碼 – 0x8badf00d 2011-12-30 15:38:15
@ 0x8badf00d added! – DexCurl 2011-12-30 15:44:21
Where is your cell.accessoryType = UITableViewCellAccessoryNone;在你的cellForRowAtIndexPath方法?並根據@samfisher答案對您的委託方法進行更改。 – 0x8badf00d 2011-12-30 16:00:56