你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> ios C 使用UINib加載xib文件實現UITableViewCell

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

編輯:關於IOS

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

  xib文件的加載過程:     1.將xib文件從磁盤載入內存,有兩種技術可以加載xib文件:NSBundle和UINib。     2.執行unarchive和initialize操作,該過程主要由NSCoding Protocol中的initWithCoder:(NSCoder *)decoder完成。     3.建立connections:Outlets和Actions。Outlets使用IBOutlet關鍵字標示,使用setValue:forKey:方法建立每個Outlet,所以每個Outlet的建立都會發送KVO通知。Actions使用IBAction關鍵字標示,替換void返回值,通過調用addTarget:action:forControlEvents:方法建立每個Action連接。注意,這裡構建Outlets和Actions是有先後順序的,先建立Outlets連接,隨後建立Actions連接。因為,Actions的建立依賴之前建立的Outlets。     4.調用awakeFromNib方法,首先要調用super的awakeFromNib方法,之後可以設置一些個性化的操作,以及一些無法在設計時設定的操作。注意,awakeFromNib消息只發往在Interface Builder中指定的Custom Class,不會發送給Files's Owner,First Responder等占位對象。     之後,該對象的加載完成,可以進行各式各樣的操作了。   使用NSBundle加載xib文件:       [[NSBundle mainBundle]loadNibNamed:<(NSString *)> owner:<(id)> options:<(NSDictionary *)>]; 這是最常見的一種,loadNibNamed:owner:options:方法返回的是一個NSArray*,裡面包含了所加載xib文件包含的界面對象(class)。   NSBundle每次都從磁盤上載入xib文件,而UINib則只是第一次從磁盤上載入xib文件,之後將xib文件緩存在內存中,每次新生成一個對象時,直接訪問內存中的xib文件執行上面描述的2-4步,所以性能上會有很大的提升,並且開發者文檔也建議對於那些重復使用的xib文件使用UINib 加載技術,當收到低內存警告時,會從內從中卸載xib文件,當再次訪問的時候會從磁盤中載入。下面看一下UINib的定義:   復制代碼 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; }
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved