Reputation: 967
In my iOS app, I have a table view and it has 2 sections/ 2 arrays. I am trying to move an object from one array/section to another array/section. Now Section 0 is called followedArray and Section 1 is called dataArray, dataArray stores all my data that make up each cell. So when the user clicks the button that I have set up called Follow, it's supposed to take that cell and remove it from dataArray/ Section 1 and insert it in followedArray/ Section 0. But when I try to do this I get an error that saids this.
Error Message
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3599.6/UITableView.m:1396
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'
Code:
TableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configuring the cell
Data * dataObject;
if (!isFiltered) {
if (indexPath.section == 0) {
dataObject = [followedArray objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1) {
dataObject = [dataArray objectAtIndex:indexPath.row];
}
}
else {
dataObject = [filteredArray objectAtIndex:indexPath.row];
}
// Loading Button
cell.followButton.tag = indexPath.row;
[cell.followButton addTarget:self action:@selector(followButtonClick:) forControlEvents:UIControlEventTouchUpInside];
cell.followButton.hidden = NO;
return cell;
}
Section Headers
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return @"Followed Data";
}
else {
return @"All Data";
}
}
Number of Rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (!isFiltered) {
if (section == 0) {
return [followedArray count];
}
else if (section == 1) {
return [dataArray count];
}
}
return [filteredArray count];
}
-----------------------------------------------------
-- This is where the action happens --
-----------------------------------------------------
Follow Button
-(void)followButtonClick:(UIButton *)sender {
// Adding row to tag
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition];
// Creating an action per tag
if (indexPath != nil)
{
NSLog(@"Current Row = %@", indexPath);
// ----- ERROR HERE -----
[self.myTableView beginUpdates];
// ----- Inserting Cell to Section 0 ----- *NOT WORKING*
[followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:indexPath.row];
NSInteger rowToAdd = indexPath.row;
[self.myTableView insertRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToAdd inSection:0], nil] withRowAnimation:YES];
// ----- Removing Cell from Section 1 ----- *WORKING*
[dataArray removeObjectAtIndex:indexPath.row];
NSInteger rowToRemove = indexPath.row;
[self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];
[self.myTableView endUpdates];
}
}
Upvotes: 0
Views: 137
Reputation: 967
This is what helped me fix the errors, Credits to @AliOmari , @Rachel & @CodeChanger
[self.myTableView beginUpdates];
[followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:0];
[myTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[dataArray removeObjectAtIndex:indexPath.row];
NSInteger rowToRemove = indexPath.row;
[self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];
[self.myTableView endUpdates];
Upvotes: 0
Reputation: 8351
Modify Follow Button Code as mention below:
Follow Button
-(void)followButtonClick:(UIButton*)sender {
//Use this IndexPath
NSInteger buttonTag = [sender tag];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:buttonTag inSection:<YourSection>];
// Creating an action per tag
if (indexPath != nil)
{
NSLog(@"Current Row = %@", indexPath);
// ----- ERROR HERE -----
[self.myTableView beginUpdates];
// ----- Inserting Cell to Section 0 -----
[followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:indexPath.row];
NSInteger rowToAdd = indexPath.row;
[self.myTableView insertRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToAdd inSection:0], nil] withRowAnimation:YES];
// ----- Removing Cell from Section 1 -----
[dataArray removeObjectAtIndex:indexPath.row];
NSInteger rowToRemove = indexPath.row;
[self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];
[self.myTableView endUpdates];
}
}
Upvotes: 1
Reputation: 2988
This might be because of convertPoint that you are using to get the indexPath of the clicked cell. Try creating a protocol in your CustomCell and implement your followButtonClick also in your CustomCell and use delegation to get the callBack in your controller with your CustomCell object and use that object to get the indexPath. Please refer the following code.
Your CustomCell.h #import
@protocol CustomCellDelegate;
@interface CustomCell : UITableViewCell
@property(nonatomic,weak) id<CustomCellDelegate> delegate;
@end
@protocol CustomCellDelegate <NSObject>
-(void)followButtonClickedWithCell:(CustomCell *) cell;
@end
Your CustomCell.m
#import "CustomCell.h"
@implementation CustomCell
...
-(void)followButtonClicked:(UIButton *) sender{
if(self.delegate != nil){
[self.delegate followButtonClickedWithCell:self];
}
}
...
@end
Confirm your view controller to implement CustomCellDelegate and in the delegate method refer the following code to get the clicked indexPath
-(void)followButtonClickedWithCell:(CustomCell *) cell{
NSIndexPath *clickedIndexPath = [self.tableView indexPathForCell:cell];
if (clickedIndexPath != nil){
//Implement your logic here
}
}
Upvotes: 0