2011-10-10 69 views
1

上重新加载我对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]]; 
    } 
} 

感谢您的帮助!

回答

1

如果单元格不为零(如果它已从表视图中出列),那么您实际上不会改变附件类型。降否则,即改变

else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 

if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
+0

感谢您的回答时间,但是应用程序崩溃,如果我这样做。这里的控制台输出http://pastie.org/2677067你可能会理解它,但我没有那么长时间到我的“训练”。 – AndHeiberg

+0

可能因为你有[NSString stringWithString:cell.textLabel.text]其中cell.textLabel.text是零,这引发了一个异常。 – jbat100

+0

TAmazing,我把if语句移到了我设置cell.text的点以下。在这一点上它工作得很好。谢谢,你刚刚救了我的一天。 – AndHeiberg

0

这听起来像你正在重用的UITableView的每次展示模态视图同一时刻,在这种情况下,发生了什么事到tableViewListChecked数组?您正在交换tableViewList中的数据;但是你是否采用了一些策略来在按钮1和按钮2之间的点击之间保持已检查项目的列表?

+0

tableViewListChecked数组是由我分配的,据我所知,它不会被删除,直到程序退出。阵列不需要在退出后保存,所以我根本不会释放它。这是我的策略,它可能不是理想的,但到目前为止它工作得很好。 感谢您的回答。 – AndHeiberg

0

如果这里的人没有帮你,你可以设置: cell.accessoryType = UITableViewCellAccessoryNone每次用户点击按钮2个

+0

我喜欢你的想法,但我不知道如何设置每个细胞accesorrytype从其他veiw没有。你能向我解释一下吗? 感谢您的回复。 – AndHeiberg

+0

严..没有测试过我的想法,但我认为你可以在viewDidLoad中定义一个布尔值并将其设置为NO。然后当用户点击button2时,将此布尔值设置为YES并将其保存在userDefaults中(如果您不知道如何显示代码),则当另一个视图出现时,请从存储器中检查此bool,并在您的cellForRowAtIndexPath方法检查这个布尔值并设置你的单元格..正如我之前提到 –

+0

这可能是一个解决方案,但我不会测试它,因为jBat已经给了我一个很好的答案,这将让我保持在模态视图控制器的所有代码:)但再次感谢 – AndHeiberg