加载本地UITableViewCell

转自:非代码方式创建UItableViewCell

我们很多时候都是通过代码的方式alloc,init一个uitableviewcell,可是有的时候却有那么一点必要来使用xib文件所见即所得的来设计布局我们的cell。下面介绍如何使用非代码的方式创建他们。

方法一:

新建一个空的nib文件,拖一个uitableviewcell到视图中。如果有必要就修改所属类为你的自定义类,拖拽各种属性关系。

然后在加载的时候使用如下代码

复制代码
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@”YOUNIBFILENAME” owner:nil options:nil];
for(id currentObject in topLevelObjects)//其实一般都是objectAtIndex 0
{
    if([currentObject isKindOfClass:[iCodeBlogCustomCell class]])
    {
        cell = (iCodeBlogCustomCell *)currentObject;
        break;
    }
}
复制代码

 

说明:参考一中说代码生成的更快,可是我测试的时候是nib加载的更快。

 

方法二:使用UINib

在需要使用这个cell的类中IBOutlet 一个这个cell的对象。只需要读取一次,然后放在内存中,所以速度会很快。

IBOutlet UITableViewCell *loadedCell;

新建一个空的nib文件,拖一个uitableviewcell到视图中。如果有必要就修改所属类为你的自定义类,拖拽各种属性关系。

然后修改File's Owner为你需要使用这个cell的类。关联IBOutlet到这个cell上。

需要加载cell时使用如下代码

[[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];

cell = loadedCell;

loadedCell = nil;

 

或者这样的代码

.h文件中

@property(nonatomic,strongUINib* topicCellNib;

@property(nonatomic,assignIBOutletTopicCell* topicCell;

。m文件

复制代码
- (UINib *)countryCellNib

{

  if (!_countryCellNib)

  {

    _countryCellNib = [UINib nibWithNibName:@"CountryCell" bundle:nil];

  }

  return _countryCellNib;

}

 

- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

  UITableViewCell *cell = [tableView

            dequeueReusableCellWithIdentifier:UYLCountryCellIdentifier];

 

  if (cell == nil)

  {

    [self.countryCellNib instantiateWithOwner:self options:nil];

    cell = self.countryCell;

    self.countryCell = nil;

  }

 

  // Code omitted to configure the cell...

 

  return cell;

}
复制代码

 

方法三:

iOS5中,可以为tableview注册一个nib绑定cell

复制代码
- (void)viewDidLoad

{

  ...

  ...

 

  UINib *countryNib = [UINib nibWithNibName:@"CountryCell" bundle:nil];

  [self.tableView registerNib:countryNib

                  forCellReuseIdentifier:UYLCountryCellIdentifier];

}

- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

  UITableViewCell *cell = [tableView

            dequeueReusableCellWithIdentifier:UYLCountryCellIdentifier];

 

  // Code omitted to configure the cell...

 

  return cell;

}
复制代码

 

 

 

方法四:

静态table view

如果使用storyboard并且创建的是uitablewviewController的子类,那么可以使用静态表视图,事先就完全定义好各个cell,这次你在写程序那个让人崩溃的繁杂的更多页面是不是顿时轻松了许多?具体示例见参考二。

注:静态的表视图只能用在UITableViewController子类中。

 

方法五:

使用storyboard,

拖个tableview到视图上,

拖个tableviewcell到tableview上,

设置tableview的代理,

设置tableviewcell的标示符

在cellforrollatindexpath中直接dequeue就可以了

复制代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString* identifier = @"cell";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    cell.textLabel.text = @"12313";

    return cell;

}
复制代码

 

可以添加多个不同样式的cell,用的时候根据不同的identifier查找就可以了。

 

 

类似的,可以用nib方式来加载UIView。

 

 

参考一:http://cocoawithlove.com/2010/03/load-from-nib-or-construct-views-in.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+CocoaWithLove+%28Cocoa+with+Love%29

参考二:http://useyourloaf.com/blog/2012/05/07/static-table-views-with-storyboards.html

参考三:http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-uitableview-tutorial.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+blogspot%2FSoFHn+%28Technology+Blog%29&utm_content=Google+Reader

参考四:http://useyourloaf.com/blog/2012/06/07/prototype-table-cells-and-storyboards.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值