NSIndexPath-UITableview

在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 Class Reference


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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容