2013-02-19 17 views
2

我有一個名爲ProductiPadCell的自定義單元類,專爲iPad設計。 (iOS 5 SDK)UITableViewCell爲不同的方向和單元類型加載自定義XIB

我想要實現的是根據當前狀態(擴展與否)和界面方向加載我的自定義單元格。我正在處理視圖控制器中的兩個條件,問題是我不喜歡它的性能。我已經NOT定義爲任何下面定義的xibs的cellIdentifier,最近我檢測到我的tableViewcellForRowAtIndexPath:

ProductiPadCell有4個不同的XIBs它們;

ProductiPadCell-Regular.xib 
ProductiPadCell-Regular-Landscape.xib 
ProductiPadCell-Expanded.xib 
ProductiPadCell-Expanded-Landscape.xib 

ProductiPadCell defition;

@interface ProductiPadCell : UITableViewCell 

@property (weak, nonatomic) IBOutlet UIButton *detailButton; 
@property (weak, nonatomic) IBOutlet UILabel *productDescriptionLabel; 
@property (weak, nonatomic) IBOutlet TTTAttributedLabel *timeLabel; 
@property (weak, nonatomic) IBOutlet TTTAttributedLabel *def1Label; 
@property (weak, nonatomic) IBOutlet TTTAttributedLabel *def2Label; 
@property (weak, nonatomic) IBOutlet UIImageView *thumbnail; 
    // appears in case the cell is expanded 
    @property (weak, nonatomic) IBOutlet UIView *detailHolderView; 
    @property (weak, nonatomic) IBOutlet UILabel *detailLabel; 
// calls a method at the superview's ViewController 
- (void)presentDetailViewWithIndex:(NSInteger)index; 
@end 

TableView中的cellForForAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // table identifier does not matches the xib's cell identifier 
    // cell identifier of all 4 xibs are "" (empty) 

    static NSString *simpleTableIdentifier = @"Cell"; 
    BOOL isExpanded = [[[targetDictionary objectForKey:@"isItemExpanded"] objectAtIndex:indexPath.row] boolValue]; 

    ProductiPadCell *cell = (ProductiPadCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) 
    { 
     // Load the required nib for the current orientation and detail selection style 
     if (deviceOrientation == UIDeviceOrientationPortrait) { 
      if (isExpanded) { 
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProductiPadCell-Regular-Expanded" owner:self options:nil]; 
       cell = [nib objectAtIndex:0]; 
       // additional settings for expanded case 
      } 
      else { 
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProductiPadCell-Regular" owner:self options:nil]; 
       cell = [nib objectAtIndex:0]; 
       // additional settings for NOT expanded case      
      } 
     } 
     else {  
      if (isExpanded) { 
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProductiPadCell-Landscape-Expanded" owner:self options:nil]; 
       cell = [nib objectAtIndex:0]; 
      } 
      else { 
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProductiPadCell-Regular-Landscape" owner:self options:nil]; 
       cell = [nib objectAtIndex:0]; 
      } 
     } 
    } 

    // connect cell information with cell IBOutlets 
    cell.productDescriptionLabel.text = [[targetDictionary objectForKey:@"key"] objectAtIndex:indexPath.row]; 
    // .... 

    [cell.thumbnail setImage:[UIImage imageNamed:@"thumb_image.jpg"]]; 
    [cell.thumbnail.layer setCornerRadius:7.0f]; 
    [cell.thumbnail.layer setBorderWidth:5.0f]; 
    [cell.thumbnail.layer setBorderColor:[UIColor clearColor].CGColor]; 
    [cell.thumbnail setClipsToBounds:YES]; 

    if (isExpanded) { 
     cell.detailLabel.text = [[targetDictionary objectForKey:@"key"] objectAtIndex:indexPath.row]; 

    } 

    return cell; 
} 

我應該怎麼做才能加載我的廈門國際銀行的,其他比我目前的做法?在每次方向更改和按鈕單擊時,我呼叫[tableView reloadData],因爲dequeueReusableCellWithIdentifier:simpleTableIdentifier未找到simpleTableIdentifier,它始終會創建新單元格。如果我將simpleIdentifier定義爲我的單元格xib,它們在大小寫更改(方向更改,擴展狀態更改)期間不會更改。

小區標識符的最佳用途是什麼?我應該使用另一種方法,而不是dequeueReusableCellWithIdentifier:simpleTableIdentifier

我在等待任何解決方案,以我目前的情況,因爲我確定一遍又一遍地加載每個單元格不是這樣做的最有效的方式。

回答

1

這是一種方式,緩存筆尖

合成這種特性

@property (nonatomic, strong) id cellNib; 

@synthesize cellNib = _cellNib; 

進行自定義的getter

-(id)cellNib 
{ 
    if (!_cellNib) 
    { 
     Class cls = NSClassFromString(@"UINib"); 
     if ([cls respondsToSelector:@selector(nibWithNibName:bundle:)]) 
     { 
      _cellNib = [[cls nibWithNibName:@"SomeCell" bundle:[NSBundle mainBundle]] retain]; 
     } 
    } 

    return _cellNib; 
} 

viewDidLoad中

[self.tableView registerNib:[UINib nibWithNibName:@"SomeCell" bundle:nil] forCellReuseIdentifier:kCellIdentification]; 

的tableView:的cellForRowAtIndexPath:

SomeCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentification]; 
    if (cell == nil) 
    { 
     //If nib cache 
     if ([self cellNib]) { 
      NSArray* viewFromNib = [[self cellNib] instantiateWithOwner:self options:nil]; 
      cell = (SomeCell *)[viewFromNib objectAtIndex:0]; 
     }else { //nib not cache 
      NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"SomeCell" owner:nil options:nil]; 
      cell = (SomeCell *)[views objectAtIndex:0]; 
     } 
    } 

希望幫助

+0

只是爲了確保我理解正確的話,我只是對我的情況下,如果在viewDidLoad中他們每個人都有不同的標識符和登記我的筆尖的所有4個(如你指出)4種不同的cellNib後,我檢查哪些筆尖我需要在tableView:cellForRowAtIndexPath:並調用dequeueReusableCellWithIdentifier:與該標識符?這是我第一次看到registerNib:forCellReuseIdentifier:並不常用。謝謝 ! – Bartu 2013-02-19 19:29:01

0

您應該使用新的方法,registerNib:forCellReuseIdentifier:註冊您的碎粒。你爲每個筆尖做了一次,在viewDidLoad中是一個不錯的地方。每個筆尖中的單元格都應該有一個重用標識符,您可以使用它來註冊筆尖,並且還可以在出隊時使用cellForRowAtIndexPath。在cellForRowAtIndexPath中使用的新方法是dequeueReusableCellWithIdentifier:forIndexPath :.使用該方法,並且不需要if(cell == nil)子句,系統將根據需要從nib創建一個子句。

相關問題