UITableView自動滾動方法


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];

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM