2012-02-02 93 views
-1

伙计们,我有从我的nsarray加载的tableview,但是在管理单元格之后,它显示了错误位置的元素。 生成细胞:在uitableview中删除和添加行

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSArray *currentArray = [comments objectAtIndex:indexPath.section]; 
    NSDictionary *currentComment = (NSDictionary *)[currentArray objectAtIndex:indexPath.row]; 

    static NSString *CellIdentifier = @"TitleCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    int commentLevel = [[currentComment objectForKey:@"level"] intValue]; 
    NSString *commentText = [currentComment objectForKey:@"text"]; 
    cell.textLabel.tag = 0xff ; 
    cell.textLabel.numberOfLines = 0; 
    cell.textLabel.font = [UIFont fontWithName:@"Verdana" size:17.0]; 
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
    cell.textLabel.text = commentText; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    if (commentLevel > 0 && cell.imageView.image == nil) { 
     cell.imageView.image = [UIImage imageNamed:@"06-arrow-northwest.png"]; 
    } 
    if (commentLevel == 0 && [[openedSections objectAtIndex:indexPath.section] boolValue] == NO) { 
     if ([currentArray count] > 1) { 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } 
    } 
    return cell; 
} 

编辑方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    if ([[openedSections objectAtIndex:indexPath.section] boolValue] == NO) { 
     [openedSections replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithInt:1]]; 
     NSMutableArray *rows = [[NSMutableArray alloc] init]; 
     int i = 1; 
     while (i < [[comments objectAtIndex:indexPath.section] count]) { 
      NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:indexPath.section]; 
      [rows addObject:[path copy]]; 
      [path release]; 
      i = i + 1; 
     } 
     [tableView beginUpdates]; 
     [tableView insertRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [tableView endUpdates]; 
     [rows release]; 
    } else if (indexPath.row == 0) { 
     [openedSections replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithInt:0]]; 
     NSMutableArray *rows = [[NSMutableArray alloc] init]; 
     int i = 1; 
     while (i < [[comments objectAtIndex:indexPath.section] count]) { 
      NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:indexPath.section]; 
      [rows addObject:[path copy]]; 
      [path release]; 
      i = i + 1; 
     } 
     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [tableView endUpdates]; 
     [rows release]; 
    } 
} 

你有什么想法?

回答

2

无论何时修改表格中的单元格,您都需要更新NSArray,否则数据源和视图将不同步。

所以每当你拨打:

[tableView insertRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic]; 

你应该把

for (NSIndexPath *indexPath in rows) 
{ 
    [self.myDataArray insertObject:myRowModel atIndex:indexPath.row]; 
} 

除了让您的视图和模型保持同步。

很明显,上面是伪代码(你必须创建一个任何模型对象的实例),你的数组必须是NSMutableArray,否则当你试图插入它时会崩溃。

+0

它不是溶剂。 NSArray“comments”包含一些NSArrays,它包含所有单元格的NSDictionaries。但nsarray“openedSections”控制它。 – werbary 2012-02-03 06:05:10