上重新加载我对iPhone编程非常陌生,所以我的问题可能是由于完全缺乏对基本原理的知识所致。Cell accessorytype不能在[tableview reloadData]
我正在开发一个有两个视图的iPhone应用程序。第一个视图有两个按钮,当按下其中一个按钮时,模式视图会弹出一个tableview。这个表格视图的填充方式取决于按下哪个按钮。如果按钮button1被按下,tableview被填充button1Data。用户可以选择单元格,如果已完成,单元格accessorytype将设置为UITableViewCellAccessoryCheckmark。然后我将选中的单元格的名称保存到tableViewListChecked中,以便稍后可以决定在数据更改时应该检查哪个单元格。
问题如下:modalview关闭后,我选择button2在button2Data中选中的单元仍然在button2Data中选中。我很清楚,由于数据确实发生了变化,函数[theTableView reloadData]正在工作,但是在我将这些单元格从屏幕上滚动之前,accesorytupes仍然是相同的。
P.S.如果我确实在屏幕上滚动单元格,那么accessorytype设置正确。
- (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];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [tableViewList objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (![tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableViewListChecked addObject:[NSString stringWithString:cell.textLabel.text]];
}
else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
cell.accessoryType = UITableViewCellAccessoryNone;
[tableViewListChecked removeObject:[NSString stringWithString:cell.textLabel.text]];
}
}
感谢您的帮助!
感谢您的回答时间,但是应用程序崩溃,如果我这样做。这里的控制台输出http://pastie.org/2677067你可能会理解它,但我没有那么长时间到我的“训练”。 – AndHeiberg
可能因为你有[NSString stringWithString:cell.textLabel.text]其中cell.textLabel.text是零,这引发了一个异常。 – jbat100
TAmazing,我把if语句移到了我设置cell.text的点以下。在这一点上它工作得很好。谢谢,你刚刚救了我的一天。 – AndHeiberg