I wanted to have some spacing between my TableView cells so I’ve changed my TableView code to this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [_boxs count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
My problem is that my “add line \ object” code is crashing. What changes should I make to this code?
Problematic code:
SomeClass *newObject = [[SomeClass alloc]initWithTitle:@".....
[_boxs addObject:newObject];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_boxs.count-1 inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:NO];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self performSegueWithIdentifier:@"FirstDetailsSegue" sender:self ];
-boxs is a NSMutableArray.
This is the error I’m getting: * Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘attempt to insert row 7 into section 0, but there are only 1 rows in section 0 after the update’
You have switched your row and column in the line where you are creating
NSIndexPath. You need to modify your code as,If you were planning to use only one section and multiple rows in that you should change your delegate methods to return only 1 section and
[_boxs count]rows. In that case your code would have worked.As per you comment, looks like you want to insert a new section and not a row. Try this,
You can remove your insertRow line from your code.