在UITableView和UICollectionView中, 经常会遇到比较两个NSIndexPath对象是否相同的情况.
错误写法
if (currentIndexPath != lastIndexPath) {
// TODO
} else {
// TODO
}
因两个NSIndexPath对象分别指向不同的内存区域, 所以一般情况下, 以上的比较方式会永远成立.
分别使用section和row/item
只能分别对NSIndexPath对象的section与row或item进行判断:
对于UITableView:
if (currentIndexPath.section != lastIndexPath.section ||
currentIndexPath.row != lastIndexPath.row) {
// TODO
} else {
// TODO
}
而对于UICollectionView: