在UITableView中有一个方法:- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
该方法里面的第一个参数是需要一个装有NSIndexPath类型数据的数组,那此处就需要创建我们需要更新的tableView所对应的IndexPath,对于创建tableview的IndexPath,你需要使用的创建方法应该是+ (instancetype)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
因为NSIndexPath类所表示的东西是一个层级关系(路径),对于tableView的indexPath它有自己独特的层级关系,+ (instancetype)indexPathForRow:(NSInteger)row inSection:(NSInteger)section该方法是官方专门为tableview设立的一个方法(对应的collectionView也同样有一个相对应的方法),该方法生成的indexPath的层级关系就是tableview中所需要的。
若使用了其它初始化方法,也需要确保所创建的indexPath的层级关系要与tableView中indexPath一致。
NSIndexPath:(官方文档解释)
TheNSIndexPathclass represents the path to a specific node in a tree of nested array collections. This path is known as anindex path.
Each index in an index path represents the index into an array of children from one node in the tree to another, deeper, node. For example, the index path1.4.3.2specifies the path shown in Figure 1.
Figure 1Index path1.4.3.2
NOTE
The UIKit framework adds programming interfaces to theNSIndexPathclass of the Foundation framework to facilitate the identification of rows and sections inUITableViewobjects and the identification of items and sections inUICollectionViewobjects. The API consists of class factory methods and properties for accessing the various indexed values. You use the factory methods to create an index path for the corresponding table view or collection view.