UITableView insertRowsAtIndexPaths errors
-
13-11-2019 - |
Question
I have looked through almost all search results, but my error doesnt go away. I have a table view initialised with 2 sections and 1 row each (from an array). I have a button for the section header view. On clicking a button I want to add a row to the 1st section. Here's the code:
- (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];
}
Heres the error:
* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046
Solution
Since you're using [arr count]
as the return value for tableView:numberOfRowsInSection:
, at the end of the beginUpdates…endUpdates
block the tableView is expecting that there will be two rows in each of your tableView sections, but your call to insertRowsAtIndexPaths:
only indicates that a row is being to section 0.
You need to fix your tableView:numberOfRowsInSection:
to return different values depending on the section:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return [arr count];
} else {
return 1;
}
}
Note that there's a lot of fishy stuff going on with your tableView: you have two sections, but you're showing the same exact row 0 in each section. And the insertion behavior within your sections is pretty wonky: you start off showing just one row, corresponding to case 0 in your switch
, then you indicate that you're inserting a row at row 0, after which you'll be showing the case 0 row at row 0 and the case 1 row at row 1. So you should really be inserting a row at row 1 instead of row 0:
NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]];
[self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];
OTHER TIPS
try with below code
NSIndexPath *indexpath_add = [NSIndexPath indexPathForRow:0 inSection:0];
[[self myTableView] beginUpdates];
[[self myTableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexpath_add] withRowAnimation:UITableViewRowAnimationRight];
[[self myTableView] endUpdates];