UITableView的scrollToRowAtIndexPath:atScrollPosition:animated的崩潰
[摘要:reason: '-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (0) beyond bounds (0) for section (0). 那個毛病是傳進的IndexPath已越界了。須要正在挪用之前到場判別語句,沒有影響機能的情]
reason: '-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (0) beyond bounds (0) for section (0).
這個錯誤是傳入的IndexPath已經越界了。需要在調用之前加入判斷語句,不影響性能的情況下,在調用之前要先reloadDate
代碼示例:
@implementation UITableView(ScrollToTopOrBottom) -(void)scrollToTopWithAnimated: (BOOL)animated{ if([selfnumberOfSections]>0&&[selfnumberOfRowsInSection: 0]>0){ [selfscrollToRowAtIndexPath: [NSIndexPathindexPathForRow: 0inSection: 0]atScrollPosition: UITableViewScrollPositionTopanimated: animated]; } } -(void)scrollToBottomWithAnimated: (BOOL)animated{ if([selfnumberOfSections]>0){ NSIntegerlastSectionIndex=[selfnumberOfSections]-1; NSIntegerlastRowIndex=[selfnumberOfRowsInSection: lastSectionIndex ]-1; if(lastRowIndex>0){ NSIndexPath*lastIndexPath=[NSIndexPathindexPathForRow: lastRowIndexinSection: lastSectionIndex]; [selfscrollToRowAtIndexPath: lastIndexPathatScrollPosition: UITableViewScrollPositionBottomanimated: animated]; } } }@end
