Nib tableViewcell的三种注册方式

以前总是混淆, 现在总结一下:


 //关于xib cell的使用
 //1.推荐,需要在xib文件中设置重用id:
//    SGSMsgeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"xxyy"];//最好在xib文件中设置id
//    if (cell == nil) {
//        cell = [[NSBundle mainBundle]loadNibNamed:@"SGSMsgeTableViewCell" owner:nil options:nil].firstObject;
//        cell.backgroundColor = SGSColorMainTableGray;
//    }
//    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//    SGSLog(@"%@",cell);
//运行时生成cell数目=data.count;
//如果不设置xib的id,也能成功,但滚动时会生成新的cell,不从重用池获取!


//2
//    [self.mainTableView registerNib:[UINib nibWithNibName:@"SGSMsgeTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"xxyy"];
    //配合使用:
//    SGSMsgeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"xxyy"];//不需在xib中注册id
    //注意,此种方式不用判断if(cell==nil),在运行时会生成cell数目=data.count    


//3 手写代码时: 需要重写cell的initWithStyle...方法
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1122"];
//    if (cell==nil) {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"1122"];
//    }
//    [cell.textLabel setText:@"wwww"];
//    SGSLog(@"%@",cell);
//编译时生成cell的个数=data.count, 滑动时重用cell
//    return cell;

猜你喜欢

转载自blog.csdn.net/james15902085063/article/details/80249170
今日推荐