非代码方式创建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,strong) UINib* topicCellNib;

@property(nonatomic,assign) IBOutletTopicCell* 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

原文地址:https://www.cnblogs.com/v2m_/p/2547721.html