2012-02-28 32 views
1

我已经浏览了几乎所有的搜索结果,但是我的错误并没有消失。我有一个表格视图初始化2节和1行(从数组)。我有一个用于部分标题视图的按钮。点击一个按钮,我想添加一行到第一部分。下面的代码:UITableView insertRowsAtIndexPaths错误

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

继承人的错误:

* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046

回答

2

由于您使用[arr count]tableView:numberOfRowsInSection:的返回值,在beginUpdates…endUpdates块的tableView期待会有结束两行都在你的每个tableView部分,但是你对insertRowsAtIndexPaths:的调用仅表示一行正在到0部分。

您需要修复您的tableView:numberOfRowsInSection:以返回不同的值,具体取决于o n个部分:

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

注意,有很多的东西腥的事情与你的tableView:你有两个部分,但你表示各部分与同一行0。而且在你的部分中的插入行为相当不错:你开始只显示一行,对应于你的switch中的情况0,那么你表明你在第0行插入一行,之后你将显示该情况0一行行0和行1的情况下,1排那么你应该在第1行被插入,而不是0行的行:

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

与下面的代码尝试

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

这不仅会导致断言失败,但它甚至不尝试向表视图添加一行,这是OP想要做的。 – yuji 2012-02-28 09:49:55

相关问题