//
// ViewController2.m
// 关于tableView加载数据之后显示到最后一行数据
//
// Created byzslon 16/7/6.
// Copyright © 2016年 冷夜孤寒. All rights reserved.
//
#import "ViewController2.h"
#import "TestCell.h"
@interface ViewController2 ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)IBOutlet UITableView * tableView;
@property (nonatomic, assign) BOOL isScrollBottom;
@property(nonatomic,strong)NSMutableArray * dataArray;
@end
@implementation ViewController2
-(NSMutableArray *)dataArray{
if (!_dataArray) {
self.dataArray = [[NSMutableArray alloc]init];
}
return _dataArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
for (NSInteger i = 0; i < 20; i++) {
NSString * string = [NSString stringWithFormat:@"%ld",i];
[self.dataArray addObject:string];
// NSLog(@"%@",self.dataArray);
}
}
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.isScrollBottom == NO) {
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataArray.count-1inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
if (indexPath.row == self.dataArray.count-1) {
self.isScrollBottom = YES;
}
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString * cellIdentifier = @"TestCell";
TestCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"TestCell" owner:self options:nil]lastObject];
}
cell.textLabel.text = self.dataArray[indexPath.row];
return cell;
}
网友评论