iPhone開發中關于Xib文件創建UITableViewCell方法
iPhone開發中關于Xib文件創建UITableViewCell是本文要介紹的內容,主要是來學習如何使用XIB文件創建UITableViewCell的幾種方法,來看本文詳細內容。
1、cell不做為controller的插口變量
首先創建一個空的xib文件,然后拖拽一個cell放在其上面,記得設置其屬性Identifier,假設設置為“mycell”,則我們在代碼中請求cell的時候就應該如下寫:
- NSString *identifier = @"mycell";
- UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier: identifier];
- if (!cell) {
- cell = [[[NSBundle mainBundle] loadNibNamed:identifier owner:self options:nil] lastObject];
- }
- return cell;
2、cell做為controller的插口變量
聲明的時候應該寫
- @property (nonnonatomic, assign) IBOutlet UITableViewCell *tvCell;
- @synthesize tvCell
創建nib文件的時候要把file owner選擇成當前的controller,然后把IBOut連接起來。
cellForRowAtIndexPath函數實現為:
- static NSString *MyIdentifier = @"MyIdentifier";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
- if (cell == nil) {
- [[NSBundle mainBundle] loadNibNamed:@"TVCell" owner:self options:nil];
- cell = tvCell;
- self.tvCell = nil;
- }
我們可以通過UINib來提高性能
使用UINib類可以大大的提高性能,正常的nib 加載過程包括從硬盤讀取nib file,并且實例化對象。但是當我們使用UINib類時,nib 文件只用從硬盤讀取一次,讀取之后,內容存在內存中。因為其在內存中,創建一序列的對象會花費很少的時間,因為其不再需要訪問硬盤。
頭文件中:
- ApplicationCell *tmpCell;
- // referring to our xib-based UITableViewCell ('IndividualSubviewsBasedApplicationCell')
- UINib *cellNib;
- @property (nonnonatomic, retain) IBOutlet ApplicationCell *tmpCell;
- @property (nonnonatomic, retain) UINib *cellNib;
viewDidLoad中:
- self.cellNib = [UINib nibWithNibName:@"IndividualSubviewsBasedApplicationCell" bundle:nil];
cellForRowAtIndexPath實現:
- static NSString *CellIdentifier = @"ApplicationCell";
- ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil)
- {
- [self.cellNib instantiateWithOwner:self options:nil];
- cell = tmpCell;
- self.tmpCell = nil;
- }
小結:iPhone開發中關于Xib文件創建UITableViewCell方法的內容介紹完了,希望通過本文的學習能對你有所幫助!