创建一个UITableView需要哪几个步骤?

(在UIViewController基础上)
**经过下面几步, 一个简单的UITabelView就能呈现在我们的眼前了, 开始吧☺

<h5>step1: 需要遵循两个协议</h5>

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>

<h5>step2: 创建UITableView并添加到视图上</h5>

UITableView *tableView =[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//添加到视图上
[self.view addSubview:tableView];

<h5>step3:设置代理</h5>

//用于布局
tableView.delegate = self;
//用于处理数据
tableView.dataSource = self;

<h5>step4:注册重用Cell</h5>

//参数一: 是一个类(当重用池中没有cell的时候开始创建cell类)
//参数二: 给重用池一个标识
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

<h5>step5:实现两个代理方法</h5>

//必须实现方法1 : 设置每个分区内的row的值
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
      return 10;
  }
//必须实现方法2 : 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//先从重用池中去取重用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//设置文本
cell.textLabel.text = [NSString stringWithFormat:@"row: %ld", indexPath.row];
cell.detailTextLabel.text = @"....";
//返回cell
return cell;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容