ipad this WORKS with breakpoints in and doesn't without... :/ - ipad

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
this seems to work with a break point on the deleteRowsAtIndexPaths: line, but when I take it out I get EXC_BAD_ACCESS and a crash. NO idea why - its the default code in there for a TableViewController - I added some code and it didn't work. Put break points in and it turned out it was that line, so stripped it right down to the original code and it still doesn't work! :( Argh...
Any ideas?
Thanks

When you delete a row in a tableView numberOfRowsInSection needs to update to return the number of rows before deleting the row minus 1, or else you get this ambiguous crash.

Related

Unable to delete row/coredata

In my app I have a Table view with values from a mapkit. I can edit the table, delete the row, but when the app starts over, the same deleted rows are still there....How can I fix this? The code for the editing:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_locations removeObjectAtIndex:[indexPath row]];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadData];
}
Any tips? Thank you
Load the data from core data using a NSFetchedResultsController.
Implement the delegate function to update your views when change occur.
When you delete something delete enqueue a block to delete it from core data. (if you are using NSPersistentContainer use performBackgroundTask:).
Don't update your views. This will happen automatically because of the delegate callback that you implemented in step 2.

UITableView commitEditingStyle:forRowAtIndexPath with ARC causes leak?

When an user deletes some table row with swipe-to-delete action,
Instruments Tool shows that the deleted UITableViewCell instance is still alive.
I used very ordinary approach that is:
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleDelete){
// Do Some Processing Model things...
[self.tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation: UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}
}
I can't sure but I think It is bug that is related with ARC.
May I leave this problem or should I have to find any walk around?
TableView holds on to the cell so it can reuse them later.

ios delete row from table in ViewController

I try to manage to delete a row from an UITable which is part on an UIViewController. I use the Edit button in the navigation bar. Hitting it will put the table rows in edit mode. But when a delete button in a row is pressed I get an error ...'Invalid update: invalid number of rows in section 0…. when using the following:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSMutableArray *work_array = [NSMutableArray arrayWithArray:self.inputValues];
[work_array removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
What do I miss here? The Apple documentation seems to be outdated somehow.
Thanks
The problem is simple. You are not updating your data model properly before deleting the row from the table.
All you do is create some new array and delete a row from that. That's pointless. You need to update the same array used by the other data source methods such as numberOfRowsInSection:.
The issue you are having is that you are not directly updating your table's data source. You first create a completely new array called work_array based on your data source (I'm assuming it's self.inputValues) and then you remove an item from it, and then try to delete a row, but your tableView's data source still contains the item you intended to remove.
All you need to do is ensure that self.inputValues is a mutable array and directly remove the object at the index for that array, like this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.inputValues removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
I hope that helps!

UITableView after deleting a row, position of remaining rows

I have created a table view in iOS and implemented delete row functionality. Everything is working. I have one doubt,
Consider the following case,
Suppose the table has 15 rows, and if I delete row no 10. Sometimes the cells from the top (1-9) animate from top to bottom to the position of row no 10. Sometimes the cells from the bottom(11-15) animate to the top. This occurs in random.
This is the code for deleting
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#">>> Entering %s <<<", __PRETTY_FUNCTION__);
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[[self contents] removeObjectAtIndex:[indexPath row]];
NSArray *indexPathsToRemove = [NSArray arrayWithObject:indexPath];
[tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationAutomatic];
}
NSLog(#"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
And between this is tutorial I am following
You can download the project from this link.
Is there anyway to control this behaviour.
Thanks in advance
You can control it with
withRowAnimation:UITableViewRowAnimationAutomatic
There are various option available
UITableViewRowAnimationBottom
UITableViewRowAnimationFade
UITableViewRowAnimationRight
...
Actual method is:
[mainTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];

How do I reference my data source to delete row from tableview using core data

I want to delete a row from my tableview. I cannot find a solution that works from the other simialr questions here.
I know I need to use the following code, But I cant seem to figure out how to properly finish this line of code to make it work.
[dailyIncomeArray removeObjectAtIndex:indexPath.row];
But what goes where is says "dailyIncomeArray"?
[what-goes-here removeObjectAtIndex:indexPath.row];
I have a getData method that populates my tableview. Next I want to be able to swipe to the left and hit that red delete button. Thanks in advance for your help!
Heres the rest of the method
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[dailyIncomeArray removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
//[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:results] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
Here's the error I'm receiving. I don't understand what this means.
No visible #interface for 'NSArray' declares the selector 'removeObjectAtIndex:'

Resources