美文网首页
根据实时加载的数据uitableview自动上浮

根据实时加载的数据uitableview自动上浮

作者: 风中飘曳的城纺 | 来源:发表于2020-04-08 14:58 被阅读0次

首先定义

@property(nonatomic,strong)NSMutableArray * dataArray;

@property (nonatomic, assign) BOOL isScrollBottom;

为了模拟实时加载的数据,我们写一个定时加载数据效果,间隔1s

NSTimer* timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];

    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

- (void)timerAction:(NSTimer*)timer {

    NSString*str = [NSStringstringWithFormat:@"%ld个大美女",(long)i];

    [self.dataArrayaddObject:str];

    i++;

    [self.textTabView reloadData];

}

在ViewDidAppear中

self.isScrollBottom = NO;

在number中

if (self.isScrollBottom) {

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

            if(self.dataArray.count>0) {

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

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

            }

        });

 }

在代理方法willDidplayCell:forRowAtIndexPath 中写入

if (self.isScrollBottom == NO) {

        [self.textTabView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataArray.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];

        if(indexPath.row==self.dataArray.count-1) {

            self.isScrollBottom=YES;

        }

    }

当然,cellForRowAtIndexPath代理方法中赋值还是需要的

static NSString *cellId = @"BOTextCell";

    BOTextCell *Cell = [tableView dequeueReusableCellWithIdentifier:cellId];

    if(Cell ==nil) {

        Cell = [[[NSBundle mainBundle]loadNibNamed:@"BOTextCell" owner:self options:nil]lastObject];

    }

    Cell.titleLab.text=self.dataArray[indexPath.row];

后面我们就可以得到想要的效果了

相关文章

网友评论

      本文标题:根据实时加载的数据uitableview自动上浮

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