通过xib加载UITableViewCell的新方式


转自http://blog.csdn.net/yohunl/article/details/19619167
我们以前通常会这样做
- ( UITableViewCell   *)tableView:( UITableView  *)tableView cellForRowAtIndexPath:( NSIndexPath  *)indexPath{
     static    NSString   *CellIdentiferId =  @"MomentsViewControllerCellID" ;
     MomentsCell   *cell = [tableView  dequeueReusableCellWithIdentifier :CellIdentiferId];
     if  (cell ==  nil ) {
        
NSArray  *nibs = [[ NSBundle   mainBundle ] loadNibNamed : @"MomentsCell"   owner : nil   options : nil ];
        cell = [nibs 
lastObject ];
        cell.
backgroundColor  = [ UIColor   clearColor ];
        

      };
        
     }

     return  cell;
}
严重注意:我们之前这么用都没注意过重用的问题,这样写,如果在xib页面没有设置 重用字符串的话,是不能够被重用的,也就是每次都会重新创建,这是严重浪费内存的,所以,需要修改啊,一种修改方式是使用如下ios5提供的新方式:
- ( void )registerNib:( UINib  *)nib forCellReuseIdentifier:( NSString  *)identifier
还有就是在xib页面设置好(ios5之前也可以用的)


NSArray * nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:nil options:nil];

for (id obj in nibObjects)
{
    if ([obj isKindOfClass:[CustomTableCell class]])
    {
        cell = obj;
        [cell setValue:cellId forKey:@"reuseIdentifier"];
        break;
    
}
还有更常用的
 
 
-(NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section
{
return[self.items count];
}
 
-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier : @"myCell"];
if( !cell)
{
cell =[[UITableViewCell alloc] initWithStyle :UITableViewCellStyleDefault reuseIdentifier : @"myCell"];
}
cell.textLabel.text =[self.items objectAtIndex :indexPath.row];
return cell;
}

但是现在有一种新的方式了,可以采用如下的方式
采用registerNib的方式,并且把设置都放在了willDisplayCell方法中了,而不是以前我们经常用的cellForRowAtIndexPath
这个方法我试了下,如果我们在xib中设置了 Identifier,那么此处的必须一致,否则会crash的
- ( UITableViewCell  *)tableView:( UITableView  *)tableView cellForRowAtIndexPath:( NSIndexPath  *)indexPath
{
    MyCustomCell * cell = [tableView dequeueReusableCellWithIdentifier:
@"myCell" ];
    
if  (!cell)
    {
        [tableView registerNib:[UINib nibWithNibName:
@"MyCustomCell"  bundle: nil ] forCellReuseIdentifier: @"myCell" ];
        cell = [tableView dequeueReusableCellWithIdentifier:
@"myCell" ];
    }
    
    
return  cell;
}

- (
void )tableView:( UITableView  *)tableView willDisplayCell:(MyCustomCell *)cell forRowAtIndexPath:( NSIndexPath  *)indexPath
{
    cell.leftLabel.text = [
self .items objectAtIndex:indexPath.row];
    cell.rightLabel.text = [
self .items objectAtIndex:indexPath.row];
    cell.middleLabel.text = [
self .items objectAtIndex:indexPath.row];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值