非代碼方式創建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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM