UITableView Animation Headache

試著忘記壹切 提交于 2019-12-03 06:06:55

问题


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.


回答1:


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?




回答2:


If you want to refresh an entire section:

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



回答3:


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];



回答4:


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.




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/1096967/uitableview-animation-headache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!