iPhone開發(fā)中關(guān)于Xib文件創(chuàng)建UITableViewCell方法
iPhone開發(fā)中關(guān)于Xib文件創(chuàng)建UITableViewCell是本文要介紹的內(nèi)容,主要是來學(xué)習(xí)如何使用XIB文件創(chuàng)建UITableViewCell的幾種方法,來看本文詳細(xì)內(nèi)容。
1、cell不做為controller的插口變量
首先創(chuàng)建一個空的xib文件,然后拖拽一個cell放在其上面,記得設(shè)置其屬性Identifier,假設(shè)設(shè)置為“mycell”,則我們在代碼中請求cell的時候就應(yīng)該如下寫:
- 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的插口變量
聲明的時候應(yīng)該寫
- @property (nonnonatomic, assign) IBOutlet UITableViewCell *tvCell;
- @synthesize tvCell
創(chuàng)建nib文件的時候要把file owner選擇成當(dāng)前的controller,然后把IBOut連接起來。
cellForRowAtIndexPath函數(shù)實(shí)現(xiàn)為:
- 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,并且實(shí)例化對象。但是當(dāng)我們使用UINib類時,nib 文件只用從硬盤讀取一次,讀取之后,內(nèi)容存在內(nèi)存中。因?yàn)槠湓趦?nèi)存中,創(chuàng)建一序列的對象會花費(fèi)很少的時間,因?yàn)槠洳辉傩枰L問硬盤。
頭文件中:
- 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實(shí)現(xiàn):
- static NSString *CellIdentifier = @"ApplicationCell";
- ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil)
- {
- [self.cellNib instantiateWithOwner:self options:nil];
- cell = tmpCell;
- self.tmpCell = nil;
- }
小結(jié):iPhone開發(fā)中關(guān)于Xib文件創(chuàng)建UITableViewCell方法的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!