2013-02-19 18 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创建一个子句。

相关问题