通过上一篇微博tableview基础 的介绍,相比大家对于tableview都有一个认识了,具体参考:
http://blog.csdn.net/lee727n/article/details/72582677
下面我们直接创建tableviewController。这样我们就省去一些步骤,不用遵守协议,设置delegate soucedata。
单元格的复用,可以有两种方式,一种注册,一种不用注册直接判断,不存在cell,就创建
- (void)viewDidLoad {
[super viewDidLoad];
//单元格复用方式2 注册复用cell
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
实现代理中的协议方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 单元格复用方式1 从空闲队列中 根据名字取出一个空闲的cell
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"张三"];
//
// //如果队列中没有取出 我们需要新创建一个Cell
// if (!cell) {
// cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"张三"];
// }
//单元格复用方式2
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
// Configure the cell...
return cell;
}