UITableView之(四):使用UITableViewCell

来源:互联网 发布:淘宝怎么分级别 编辑:程序博客网 时间:2024/06/05 09:10

创建使用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];


0 0
原创粉丝点击