1

我浏览了几乎所有的搜索结果,但我的错误并没有消失。我有一个表格视图,每个部分初始化为 2 个部分和 1 行(来自一个数组)。我有一个用于节标题视图的按钮。单击按钮时,我想在第一部分添加一行。这是代码:

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    UITableViewCell *cell = (UITableViewCell*)[self.myTableView dequeueReusableCellWithIdentifier:@"DetailCell"];
    cell.textLabel.text=[arr objectAtIndex:indexPath.row];
    cell.backgroundColor=[UIColor clearColor];
    return cell; 
} 

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            btnReleases=[UIButton buttonWithType:UIButtonTypeCustom];
            [btnReleases setFrame:CGRectMake(10, 0, 30, 39)];
            [btnReleases setImage:[UIImage imageNamed:@"buttonr.png"] forState:UIControlStateNormal];
            [btnReleases addTarget:self action:@selector(loadReleases) forControlEvents:UIControlEventTouchUpInside];
            return btnReleases;
            break;
        case 1:
            btnLinks=[UIButton buttonWithType:UIButtonTypeCustom];
            [btnLinks setFrame:CGRectMake(10, 0, 30, 39)];
            [btnLinks setImage:[UIImage imageNamed:@"buttonl.png"] forState:UIControlStateNormal];
            [btnLinks addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
            return btnLinks;
            break;
        default:
            break;
    }

}
-(void)loadReleases
{

    [self.myTableView beginUpdates];
        [arr addObject:@"WTF"];
    NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; NSLog(@"%@",insert0);
    [self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];
    [self.myTableView endUpdates];
}

继承人的错误:

* -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046 中的断言失败

4

2 回答 2

2

由于您将[arr count]其用作 的返回值tableView:numberOfRowsInSection:,因此在块的末尾,beginUpdates…endUpdatestableView 预计您的每个 tableView 部分中会有两行,但您的调用insertRowsAtIndexPaths:仅表明一行是第 0 部分。

您需要tableView:numberOfRowsInSection:根据部分修复您以返回不同的值:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return [arr count];
    } else {
        return 1;
    }
}

请注意,您的 tableView 有很多可疑的东西:您有两个部分,但您在每个部分中显示相同的第 0 行。而且您的部分中的插入行为非常不稳定:您开始只显示一行,对应于您的案例 0 switch,然后您表明您在第 0 行插入一行,之后您将显示案例 0第 0 行的第 1 行和第 1 行的第 1 行。所以你真的应该在第 1 行而不是第 0 行插入一行:

NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]]; 
[self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];
于 2012-02-28T09:42:48.453 回答
0

试试下面的代码

NSIndexPath *indexpath_add = [NSIndexPath indexPathForRow:0 inSection:0];
     [[self myTableView] beginUpdates];
     [[self myTableView]  deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexpath_add] withRowAnimation:UITableViewRowAnimationRight];
     [[self myTableView] endUpdates];
于 2012-02-28T09:45:09.943 回答