在uitableview中删除和添加行

时间:2012-02-02 18:47:24

标签: objective-c ios xcode uitableview

伙计们,我有tableview,它是从我的nsarray加载的,但在管理单元格后,它会在不正确的位置显示元素。 生成细胞:

- (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];
    }
}

你有什么想法吗?

1 个答案:

答案 0 :(得分:2)

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

所以每当你打电话:

[tableView insertRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic];

你应该把

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

以便您的视图和模型保持同步。

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