我編程一個UITableView表現爲一個包容性的選擇列表。我的表格正確顯示並允許用複選框選擇多個單元格。我的問題是,已經選擇的單元格(單元格右側有一個複選標記)在滾動出視圖時鬆開了它們的選定狀態(單元格複選標記消失)。我希望即使單元格滾動出視圖,對錶格中單元格的選擇也會被保留。有誰知道這是什麼原因造成的?問題與配置的UITableView的包容性選擇列表
這裏是我的代碼我TableViewController類的內部:
- (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] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [widgetTitles_glob objectAtIndex:row];
cell.detailTextLabel.text = @"";
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// Reflect selection in data model
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
// Reflect deselection in data model
}
}
任何幫助將是非常讚賞。
應該不是如果(細胞==零)條件保存表中的所有單元格,一旦他們被分配? – Sabobin 2010-12-01 16:54:49
你不應該只存儲indexPath.row,你應該存儲整個indexPath。如果你檢查'if([selectedRows containsObject:indexPath.row])cell.accessoryType = UITableViewCellAccessoryCheckmark; 其他 單元格。accessoryType = UITableViewCellAccessoryNone;'那麼當您滾動時,每個部分中的所有第一行將最終被複選標記。你應該保存整個indexPath,因爲它同時包含了section和row。 – tazboy 2011-08-10 03:20:50