Question

NSIndexPath* updatedPath = [NSIndexPath indexPathForRow: 0 inSection: 0]; 
NSIndexPath* updatedPath2 = [NSIndexPath indexPathForRow: 1 inSection: 0]; 
NSArray* updatedPaths = [NSArray arrayWithObjects:updatedPath, updatedPath2, nil]; 
[self.mySexyTableView reloadRowsAtIndexPaths:updatedPaths withRowAnimation:UITableViewRowAnimationTop];

The above code works and it animates. The problem is that I have lot of data and I don't want to hard-code the row indexes. Since I have only one section in my table section can be 0. How can I dynamically generate NSArray of NSIndexPath objects?

Or is there an easier way to animate the table view. All the rows in my table view change when the user clicks the tab on top of the table view.

Was it helpful?

Solution

To generate the array of index paths, you could just loop:

    NSMutableArray *updatedPaths = [NSMutableArray array];
    for (NSNumber *row in someArray) {
        NSIndexPath *updatedPath = [NSIndexPath indexPathForRow:[row intValue] inSection:0];
        [updatedPaths addObject:updatedPath];
    }
    [self.mySexyTableView reloadRowsAtIndexPaths:updatedPaths withRowAnimation:UITableViewRowAnimationTop];

If you reload all data in your TableView, why not just call the reloadData method?

OTHER TIPS

If you want to refresh an entire section:

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];

If you have more than one indexPathForRow with equal row index, you get this exception.

For erample

[segmentTable reloadRowsAtIndexPaths: [[NSArray alloc] initWithObjects: 
                                [NSIndexPath indexPathForRow: 1 inSection: 1], 
                                [NSIndexPath indexPathForRow: 1 inSection: 1], nil]                                 withRowAnimation:UITableViewRowAnimationNone];

I finally got it working. Here is the code:

NSMutableArray *indexPaths = [[[NSMutableArray alloc] init] autorelease];
for (int i = 0; i < [mySexyList count]; i++) {
   NSIndexPath *updatedPath = [NSIndexPath indexPathForRow:i inSection:0];
   [indexPaths addObject:updatedPath];
}

[self.myFunkyTableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];

The problem was that I was returning hard coded value from numberOfRowsInSection: method which was not the same as mySexyList size. This was crashing the app.

Assume self.arTableData is a mutable array of table & tableView is an instance of UITableView. Assume there are 10 rows ( which means 10 objects in array ) in tableView. I do implement following code to remove rows animated.

int i; // to remove from 3 to 6
for(i=2;i<6;i++)
{
    [self.arTableData removeObjectAtIndex:i];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
}

That trick works for me without any hassles. Hope it work same for you. Best of luck.

Just to add my solution into the mix as it was simple once I found it. What was happening was that I have a table that is "grouped" by my own code using the same technique found at: cocoanetics.com

When the user does something requiring an update to a cell, and the table wasn't grouped at that time, my code was trying to use

[[self tableView] reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationAutomatic];

specifying paths to both the cell and the non-existent header. This caused the same error others have reported above. All I had to do was ensure that the "paths" provided to reloadRowsAtIndexPaths:withRowAnimation: actually existed. That done, all was fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top