美文网首页
iOS 视图出现时自动滚动到底部 且滑动卡顿解决

iOS 视图出现时自动滚动到底部 且滑动卡顿解决

作者: 前路星辰大海 | 来源:发表于2020-07-06 18:35 被阅读0次

    1.在TableView的numberOfRowsInSection方法中设置contentOffSet- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{

        if (self.isScrollBottom) { //只在初始化的时候执行

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0005 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                     if(self.messages.count>0) {

                        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([self.messageTable numberOfRowsInSection:0]-1) inSection:0];

                        [self.messageTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];

                     }

            });

        }

        return self.messages.count;

    }

    2.这种方法要加一个延时,不然会造成死循环。

    在viewDidAppear中将属性改为NO

    - (void)viewDidAppear:(BOOL)animated

    {

        [super viewDidAppear:YES];

        self.isScrollBottom = NO;

    }

    3.解决向上拉卡顿 缓存行高

    - (CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath{

        CGFloat height;

        if(self.heightArray.count> indexPath.row) {

            // 如果有缓存的高度,取出缓存高度

            height = [self.heightArray[indexPath.row]floatValue];

        }else{

            // 无缓存高度,计算高度,并加入数组

            XJMessage*currentMsg =self.messages[indexPath.row];

            height = currentMsg.messageHeight;

            [self.heightArray addObject:[NSNumber numberWithDouble:height]];

        }

        returnheight;

    }

    相关文章

      网友评论

          本文标题:iOS 视图出现时自动滚动到底部 且滑动卡顿解决

          本文链接:https://www.haomeiwen.com/subject/kbygqktx.html