您的位置:首页 > 产品设计 > UI/UE

UITableView之(四):使用UITableViewCell

2016-09-12 10:24 399 查看
创建使用tableViewCell有两种方式:

1、创建tableViewCell:常应用于代码创建

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *const cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:cellId];
}

// 设置cell的相关属性

return cell;
}
2、注册tableViewCell:常应用于Xib/StoryBoard创建

在创建tableView的时候就进行cell的注册:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId]; // 代码创建
[self.tableView registerNib:[UINib nibWithNibName:@"创建xib设置的控件名" bundle:nil] forCellReuseIdentifier:cellId]; // xib创建

使用注册创建tableViewCell,tableView的代理方法中,获取可重用的tableViewCell使用如下方法:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: