我已经浏览了几乎所有的搜索结果,但是我的错误并没有消失。我有一个表格视图初始化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
这不仅会导致断言失败,但它甚至不尝试向表视图添加一行,这是OP想要做的。 – yuji 2012-02-28 09:49:55