views:

3509

answers:

4
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.

+2  A: 

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?

drvdijk
reloadData does not animate.
bparanj
When I run your code I get this :*** Assertion failure in -[_UITableViewUpdateSupport _setupAnimationsForNewlyInsertedCells], /SourceCache/UIKit/UIKit-963.10/UITableViewSupport.m:5412009-07-08 09:14:42.979 QuartzFun[3781:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempt to create two animations for cell'2009-07-08 09:14:42.979 QuartzFun[3781:207] Stack: (
bparanj
If I loop :NSMutableArray *updatedPaths = [[NSMutableArray alloc] init]; for (int i = 0; i < [lowList count]; i++) { NSIndexPath *updatedPath = [NSIndexPath indexPathForRow:i inSection:0]; [updatedPaths addObject:updatedPath]; } [self.mySexyTableView reloadRowsAtIndexPaths:updatedPaths withRowAnimation:UITableViewRowAnimationTop];I get: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableIndexSet addIndexesInRange:]: Range {214747, 1} exceeds maximum index value of NSNotFound - 1' QuartzFun[3810:207]
bparanj
You could try if this works for only 5 rows or something? In the for loop, increment a counter, and when it hits 5, break. Does it work then?
drvdijk
A: 

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.

bparanj
A: 

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];
+1  A: 

If you want to refresh an entire section:

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