2012-03-15 64 views
1

我正在将一堆国家加载到一个表格并允许用户删除。这些行直接对应国家数组,通过使用objectAtIndex:indexPath.row并将删除按钮的标签设置为相同的索引。为什么UITableViewCell不能正确地重新加载?

当我删除第一对行时没关系,但之后错误的行被删除,应用程序崩溃。

下面是我如何创建行:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell; 
NSString *currentText; 

// fill in country cell content 
if ([tableView tag] == 400) { 
    cell = [tableView dequeueReusableCellWithIdentifier:@"SettingsCountryCell"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:@"SettingsCountryCell"]; 
     NSLog(@"is nil"); 
    } // it´s never nil 

    currentText = [arrSelectedCountries objectAtIndex:indexPath.row]; 
    int currentRow = indexPath.row; 

    UILabel *countryLabel = (UILabel *)[cell viewWithTag:401]; 
    countryLabel.text = currentText; 

    // create button 
    UIButton *countryButton = (UIButton *)[cell viewWithTag:402]; 

    [countryButton setTag:currentRow]; 
    [countryButton setTitle:[NSString stringWithFormat:@"row%d", currentRow] 
        forState:UIControlStateNormal]; 
    NSLog(@"%@ - tag %d - title %@ - button tag %d", 
      currentText, 
      currentRow, 
      countryButton.titleLabel.text, 
      countryButton.tag); 

    // on load: Bulgaria - tag 2 - title row2 - button tag 2 
    // after one deletion (and ever after): 
    //  Bulgaria - tag 1 - title (null) - button tag 0 

    [countryButton addTarget:self 
         action:@selector(removeCountry:) 
      forControlEvents:UIControlEventTouchUpInside]; 

}  

下面是如何删除它们(即国家):

- (void)removeCountry:(UIButton *)countryButton 
{ 
// removed country 
NSString *currentText = [arrSelectedCountries objectAtIndex:countryButton.tag]; 

NSLog(@"removed country %@ at tag %d", currentText, countryButton.tag); 

// ok: removed country All CEE Countries at tag 0 
// ok: removed country Bulgaria at tag 0 
// not ok: removed country Czech Republic at tag 1 

// add to picker selection and remove from selected 
[arrCountries addObject:currentText]; 
[arrSelectedCountries removeObject:currentText]; 

// refresh picker and country table 
[countryPicker reloadAllComponents]; 

[countryTable reloadData]; 

[countryTable reloadSections:[NSIndexSet indexSetWithIndex:0] 
      withRowAnimation:UITableViewRowAnimationFade]; 

// position table and elements below it 
[self repositionForCountryChange]; 
} 

起初,该表显示:ROW0,ROW1,ROW2等

经过几次删除:row1,row2,row3等

一旦发生这种情况,la bel不再适合标签。当我点击克罗地亚删除,删除按钮标记为第1行,说的NSLog:

removed country Czech Republic at tag 1

...并没有被删除克罗地亚行虽然其删除按钮现在说2行。

而且为什么我从获得的NSLog:

title (null) - button tag 0 

...当我可以有一个标题和removeCountry可以访问按钮标签的表格中看到,虽然不正确?

回答

0

您已经设置了带有标签的按钮,以便您将其识别为单元格中的子视图(402),然后更改标签,以便标识标签所在的行。 - 已使用,没有带标签402的视图,因此您的按钮仍以其原始行的标签号标识。

您正在使用标签有两个目的。理想情况下,不要使用它们,因为它们很脆弱,不会让你的代码可读。

  • 创建一个UITableViewCell子类,并使用插座
  • 使用我描述here方法比标签更灵活,并保持如果删除行工作确定按下的按钮的行识别其子视图。
+0

谢谢!现在正在工作。你说根本不使用标签,但正如你所看到的,我使用标签来标识表 if([tableView tag] == 400)是否有更好的方法呢? – Cekk 2012-03-16 02:54:00

+0

假设你在视图控制器中有多个tableView,在一个ivar中持有对它的引用或者有一个对它的出口,然后有'if(tableView == self.countryTable)'或者其他任何东西但是如果它现在工作正常,不觉得你必须改变它! – jrturton 2012-03-16 07:24:04

相关问题