0

我正在尝试在事件 tableView:didSelectRowAtIndexPath 上点击的单元格下方添加一个新单元格

这些是我的 numberOfSectionsInTableView 和 numberOfRowsInSection 方法。属性 self.dataTable 是我的 TableView 的数据源。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //i have that - 1 there because i don't want to use the first row of my dataTable.
    //On my cellForRowAtIndex method I'm adding a +1 to the indexPath.row to get the right row information
    return [self.dataTable count] - 1;
}

我的添加方法是:

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSMutableArray * newRow = [NSMutableArray arrayWithObjects:@"Columna 1", @"Columna 2", @"Columna 3", @"Columna 4", nil];
    //here im adding the new item to the datasource.
    [self.dataTable insertObject:newRow atIndex:indexPath.row + 1];                       

    NSArray *newData = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:(indexPath.row + 1) inSection:1], nil];
    //self because this clas inherits from UITableView and implements its own delegate and datasource.
    //The error is also produced using tableView instead of self.
    [self beginUpdates];

    [self insertRowsAtIndexPaths:newData withRowAnimation:UITableViewRowAnimationBottom];

    [self endUpdates];

}

当调试器到达 [self endUpdates] 时,方法 numberOfRowsInSection 被启动,我可以看到返回更新为 +1,但是我收到此错误:

异常名称:NSInternalInconsistencyException 原因:无效的表视图更新。应用程序已请求更新与数据源提供的状态不一致的表视图。

编辑:添加 cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"SWTableCell";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    UILabel *lblTemp; //Used for creates labels for cell
    CGRect readjust, rectTemp;
    rectTemp = CGRectMake(0, 0, self.frame.size.width, 15); //Size of the cell


    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.frame = rectTemp;

    int x = 10; //initial x position for each label
    for(int i = 0; i < self.columnCount; i++)
    {
        rectTemp = CGRectMake(x, 10, COLUMN_WIDTH, 20); //Size of each column

        lblTemp = [[UILabel alloc] initWithFrame:rectTemp]; //inicialize label
        lblTemp.tag = i;
        [lblTemp setUserInteractionEnabled:NO];
        lblTemp.text = (NSString *)[[dataTable objectAtIndex:indexPath.row + 1] objectAtIndex:i];
        lblTemp.autoresizingMask= UIViewAutoresizingFlexibleRightMargin;
        [lblTemp setHighlightedTextColor:[UIColor whiteColor]];
        if(i!=0) lblTemp.textAlignment = UITextAlignmentRight;
        else lblTemp.textAlignment = UITextAlignmentLeft;

            lblTemp.font = [UIFont systemFontOfSize:10];
            [cell setUserInteractionEnabled:YES];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        [lblTemp setNumberOfLines:1];
        [lblTemp sizeToFit];

        readjust = lblTemp.frame;
        readjust = CGRectMake(readjust.origin.x, readjust.origin.y, COLUMN_WIDTH, readjust.size.height);
        lblTemp.frame = readjust;


        [cell.contentView addSubview:lblTemp];
        [lblTemp release];

        x = x + COLUMN_WIDTH;
    }
    }
    return cell;
}

还有什么可能导致此问题?

4

1 回答 1

0

我认为

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataTable count] - 1;
}

应该

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataTable count];
}

集合的找到或长度是它包含的项目数。只有索引从 0 开始。因此,如果您将 5 个项目放入一个数组中,则它的计数为 5,而最后一个项目的索引为 4。

现在我也瞎了:

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //…
    [self beginUpdates];
    [self insertRowsAtIndexPaths:newData withRowAnimation:UITableViewRowAnimationBottom];
    [self endUpdates];
}

一定是

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //…
    [tableView beginUpdates];
    [tableView insertRowsAtIndexPaths:newData withRowAnimation:UITableViewRowAnimationBottom];
    [tableView endUpdates];
}
于 2012-04-19T18:38:23.970 回答