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

首先定义

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


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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容