IOS入门之UITableView

今天入门了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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,614评论 25 709
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,361评论 30 472
  • 在傍晚的时候,我一直在苦恼,今晚要做什么幻灯片?期间用幻灯片做了个人名片,还做了临摹练习(2张),但是后面我决定做...
    黄显浩阅读 5,371评论 6 7
  • 看着潘莹莹一脸萎靡不振的样子,卓凤图倒了一杯酒给她:“还困呐?能不能有个年轻人应该有的样子啊?” 潘莹莹打着哈欠:...
    张大茴阅读 7,338评论 0 1
  • 1、请具体描述一下你理想中的女朋友。 2、简单描述下你对未来生活的设想。 3、交往中,你最不能接受的是什么?
    诗水年华阅读 1,501评论 3 0