UITableView insertRowsAtIndexPaths错误
-
13-11-2019 - |
题
我已经浏览了几乎所有的搜索结果,但我的错误并没有消失。我有一个用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
解决方案
既然你在用 [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。并且在您的部分中的插入行为非常不稳定:你开始只显示一行,对应于你的案例0。 switch
, ,然后您指示您在第0行插入一行,之后您将在第0行显示案例0行,在第1行显示案例1行。所以你真的应该在第1行而不是第0行插入一行:
NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]];
[self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];
其他提示
使用以下代码
NSIndexPath *indexpath_add = [NSIndexPath indexPathForRow:0 inSection:0];
[[self myTableView] beginUpdates];
[[self myTableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexpath_add] withRowAnimation:UITableViewRowAnimationRight];
[[self myTableView] endUpdates];
. 不隶属于 StackOverflow