NSIndexPath UITableView移动到下一个行

UITableView 返回的当前行是 NSIndexPath 怎么移动到下一行呢?

1。 直接调用没有这样的函数,所以只能获取当前indexPath的 indeces,这个就是IndexPath的本质数据。这里indeces是使用的 C array方式存放的 (文档是这么写的)

NSUInteger* indeces = (NSUInteger*) malloc([currentIndexPath length] * sizeof(NSUInteger));
    memset(indeces, 0, [currentIndexPath length] * sizeof(NSUInteger));
    
    [currentIndexPath getIndexes:indeces];


2在 indeces[1] 中存放的是row 所以+1, 但是注意检查边界到最后一个 rowCount 需要自己从tableView的datasource取出来。
indeces[1] = ((currentIndexPath.row + 1) == rowCount ? currentIndexPath.row : currentIndexPath.row + 1);


3 生成nextIndexPath
NSIndexPath* nextIndexPath = [[NSIndexPath alloc] initWithIndexes:indeces length:[currentIndexPath length]];


4 释放所有内存
[nextIndexPath release];
    free(indeces);
    indeces = NULL;

另外 还有两种比较简单点的方法。

二 

[NSIndexPath indexPathForRow:(currentIndexPath.row + 1) inSection:>currentIndexPath.section

用这个直接创建出 indexPath。 但是 这样按照object-c编程中的建议, iphone中最好不要使用 autorelease的分配。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值