IOS入门之UITableView

2017-02-04  本文已影响42人  爆发小宇

今天入门了UITableView,跟Android的ListView很类似

直接上代码了

在.h文件中要实现两个接口,分别是UITableViewDataSource,UITableViewDelegate

以下代码是在.m文件中

static NSString *identifier = @"LinkerCell";
// 初始化tableview
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
  // 注册nib文件
    [self.tableView registerNib:[UINib nibWithNibName:@"LinkerCell" bundle:nil] forCellReuseIdentifier:identifier];
}

然后实现三个方法

// 返回data中的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (mData) {
        return mData.count;
    }
    return 0;
}
// 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LinkerCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    [cell setLinker:[mData objectAtIndex:indexPath.row]];
    return cell;
}

// 返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90.0f;
}
上一篇下一篇

猜你喜欢

热点阅读