本文共 603 字,大约阅读时间需要 2 分钟。
原来项目中一直用这个方法滚动回到首页:
NSIndexPath *bottomIndexPath=[NSIndexPath indexPathForItem:0 inSection:0];[self.collectionView scrollToItemAtIndexPath:bottomIndexPath atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
上面代码是collectionView滚动到第一区第一个,直到有一次应用崩溃,发现第一区没有item!! 尼玛,怎么办!!!
最后,找到下面代码,这样应用的健壮性又提高了,妈妈在也不用担心数据为空的情况啦!!
//CGRectMake(0, 0, 1, 1)可以直接返回到UITableView的最顶端[tableview scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];//CGRectMake(0, 0, 0, 0)设定无效//[tableview scrollRectToVisible:CGRectMake(0, 0, 0, 0) animated:NO];
上面方法,tableView/collectionView下都有效,直接复用!!!
注:本文首发于 ,如若转载,请注明来源。