ios – 使用UINib加載xib文件實現UITableViewCell

xib文件的實質是xml,描述界面對象,每一個對象都有一個很重要的屬性,identity inspector面板中class屬性,加載xib文件的時候其實是實例化界面對象相對應的這些class。緩存

xib文件的加載過程:ide

  1.將xib文件從磁盤載入內存,有兩種技術能夠加載xib文件:NSBundle和UINib。性能

  2.執行unarchive和initialize操做,該過程主要由NSCoding Protocol中的initWithCoder:(NSCoder *)decoder完成。ui

  3.創建connections:Outlets和Actions。Outlets使用IBOutlet關鍵字標示,使用setValue:forKey:方法創建每一個Outlet,因此每一個Outlet的創建都會發送KVO通知。Actions使用IBAction關鍵字標示,替換void返回值,經過調用addTarget:action:forControlEvents:方法創建每一個Action鏈接。注意,這裏構建Outlets和Actions是有前後順序的,先創建Outlets鏈接,隨後創建Actions鏈接。由於,Actions的創建依賴以前創建的Outlets。atom

  4.調用awakeFromNib方法,首先要調用super的awakeFromNib方法,以後能夠設置一些個性化的操做,以及一些沒法在設計時設定的操做。注意,awakeFromNib消息只發往在Interface Builder中指定的Custom Class,不會發送給Files's Owner,First Responder等佔位對象。spa

  以後,該對象的加載完成,能夠進行各式各樣的操做了。設計

使用NSBundle加載xib文件:代理

 

[[NSBundle mainBundle]loadNibNamed:<(NSString *)> owner:<(id)> options:<(NSDictionary *)>];

這是最多見的一種,loadNibNamed:owner:options:方法返回的是一個NSArray*,裏面包含了所加載xib文件包含的界面對象(class)。code

NSBundle每次都從磁盤上載入xib文件,而UINib則只是第一次從磁盤上載入xib文件,以後將xib文件緩存在內存中,每次新生成一個對象時,直接訪問內存中的xib文件執行上面描述的2-4步,因此性能上會有很大的提高,而且開發者文檔也建議對於那些重複使用的xib文件使用UINib 加載技術,當收到低內存警告時,會從內從中卸載xib文件,當再次訪問的時候會從磁盤中載入。下面看一下UINib的定義:orm

複製代碼
NS_CLASS_AVAILABLE_IOS(4_0) @interface UINib : NSObject {
  @private
    id storage;
}

// If the bundle parameter is nil, the main bundle is used.
// Releases resources in response to memory pressure (e.g. memory warning), reloading from the bundle when necessary.
+ (UINib *)nibWithNibName:(NSString *)name bundle:(NSBundle *)bundleOrNil;

// If the bundle parameter is nil, the main bundle is used.
+ (UINib *)nibWithData:(NSData *)data bundle:(NSBundle *)bundleOrNil;

// Returns an array containing the top-level objects from the NIB.
// The owner and options parameters may both be nil.
// If the owner parameter is nil, connections to File's Owner are not permitted.
// Options are identical to the options specified with -[NSBundle loadNibNamed:owner:options:]
- (NSArray *)instantiateWithOwner:(id)ownerOrNil options:(NSDictionary *)optionsOrNil;
@end
複製代碼

前面兩個方法很清楚,分別以不一樣的方式載入,第三個方法則是實例化(將對象建立出來)

表視圖實例:

具體的細節就不說了

建立一個名爲Empty的xib文件

9ABC0D43-DDDA-401A-8DCA-30264F5C2CDB

注意看Table View Cell的class屬性是TableViewCell,不是默認的UITableViewCell,TableViewCell.h以下:

@interface TableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *lb1;
@property (weak, nonatomic) IBOutlet UILabel *lb2;
- (IBAction)bt:(id)sender;
@end

三個屬性都和xib文件進行了連接,應該能看出來。

而後在UITableViewDataSource代理中分別進行以下操做:

//頭文件內聲明
UINib *nib;
//實例化
self->nib = [UINib nibWithNibName:@"Empty" bundle:nil];

而後在來看tableView:cellForRowAtIndexPath:方法,這個方法標準的實現方法以下:

複製代碼
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = [NSString stringWithFormat:@"Cell"];
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  }
  //config the cell
  return cell;
}
複製代碼

這是使用代碼的方式建立cell ,下面看使用UINib:

複製代碼
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];
    }
    
    switch (indexPath.section) {
        case redSection:
            cell.lb1.text = @"lb1";
            cell.lb2.text = @"lb2";
            break;
        case blueSection:
            break;
        default:
            [[cell textLabel] setText:@"Unknow"];
    }
    return cell;
}
複製代碼

效果圖以下:

7B576949-8D4E-4584-BC0E-2DB65F6CF6C1

以上只是我的理解,有錯誤之處歡迎指正。

相關文章
相關標籤/搜索