UITableView自动滚动方法

时间:2021-02-16 15:47:33

1:

NSUInteger rowCount = [self.tableView numberOfRowsInSection:0];

NSIndexPath* indexPath = [NSIndexPath indexPathForRow:rowCount-1 inSection:0];

[_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

2:

if (_tableView.contentSize.height < _tableView.height) return;//开始内容的大小没有超过tableView的高时不需要滚动

[UIView animateWithDuration:0.6 animations:^{

_tableView.contentOffset = CGPointMake(0, _tableView.contentSize.height - _tableView.height);

}];

获取当前屏幕内的cell

/**

*  获取显示的最后一条消息的indexPath

*

*  @return indexPath

*/

- (NSIndexPath *)getLastIndexPathForVisibleItems

{

NSArray *visiblePaths = [self.audienceHead indexPathsForVisibleItems];

if (visiblePaths.count == 0) {

return nil;

}else if(visiblePaths.count == 1) {

return (NSIndexPath *)[visiblePaths firstObject];

}

NSArray *sortedIndexPaths = [visiblePaths sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

NSIndexPath *path1 = (NSIndexPath *)obj1;

NSIndexPath *path2 = (NSIndexPath *)obj2;

return [path1 compare:path2];

}];

return (NSIndexPath *)[sortedIndexPaths lastObject];

}