【问题标题】:UITableView insertRowsAtIndexPaths errorsUITableView insertRowsAtIndexPaths 错误
【发布时间】:2012-02-28 17:45:09
【问题描述】:

我浏览了几乎所有的搜索结果,但我的错误并没有消失。我有一个表视图,每个部分初始化为 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

【问题讨论】:

    标签: iphone uitableview rows assertion


    【解决方案1】:

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

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

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

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

    NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]]; 
    [self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];
    

    【讨论】:

      【解决方案2】:

      试试下面的代码

      NSIndexPath *indexpath_add = [NSIndexPath indexPathForRow:0 inSection:0];
           [[self myTableView] beginUpdates];
           [[self myTableView]  deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexpath_add] withRowAnimation:UITableViewRowAnimationRight];
           [[self myTableView] endUpdates];
      

      【讨论】:

      • 这不仅会导致断言失败,而且它甚至不会尝试向表视图添加一行,而这正是 OP 想要做的。
      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多