在UITableView中有一個(gè)方法:- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
該方法里面的第一個(gè)參數(shù)是需要一個(gè)裝有NSIndexPath類型數(shù)據(jù)的數(shù)組窟她,那此處就需要?jiǎng)?chuàng)建我們需要更新的tableView所對應(yīng)的IndexPath,對于創(chuàng)建tableview的IndexPath榨呆,你需要使用的創(chuàng)建方法應(yīng)該是+ (instancetype)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
因?yàn)镹SIndexPath類所表示的東西是一個(gè)層級(jí)關(guān)系(路徑)倦炒,對于tableView的indexPath它有自己獨(dú)特的層級(jí)關(guān)系,+ (instancetype)indexPathForRow:(NSInteger)row inSection:(NSInteger)section該方法是官方專門為tableview設(shè)立的一個(gè)方法(對應(yīng)的collectionView也同樣有一個(gè)相對應(yīng)的方法)刁卜,該方法生成的indexPath的層級(jí)關(guān)系就是tableview中所需要的志电。
若使用了其它初始化方法,也需要確保所創(chuàng)建的indexPath的層級(jí)關(guān)系要與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.