OGeek|极客世界-中国程序员成长平台

标题: ios - UITableViewCell 为不同的方向和单元格类型加载自定义 XIB [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 08:52
标题: ios - UITableViewCell 为不同的方向和单元格类型加载自定义 XIB

我有一个名为 ProductiPadCell 的自定义单元格类,专为 iPad 设计。 (iOS 5 SDK)

我想要实现的是根据当前状态(扩展与否)和界面方向加载我的自定义单元格。我在 View Controller 中处理这两种情况,问题是我不喜欢它的性能。我NOT为下面定义的任何xibs定义了一个cellIdentifier,最近我检测到我的tableViewcellForRowAtIndexPath:

ProductiPadCell 有 4 个不同的 XIB;

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

ProductiPadCell 定义;

@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)presentDetailViewWithIndexNSInteger)index;
@end

TableView 的 cellForForAtIndexPath

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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"roductiPadCell-Regular-Expanded" owner:self options:nil];
                cell = [nib objectAtIndex:0];
                // additional settings for expanded case
            }
            else {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed"roductiPadCell-Regular" owner:self options:nil];
                cell = [nib objectAtIndex:0];
                // additional settings for NOT expanded case                    
            }   
        }
        else {     
            if (isExpanded) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed"roductiPadCell-Landscape-Expanded" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            else {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed"roductiPadCell-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;
}

除了我目前的方法之外,我应该怎么做才能加载我的 xib?在每次更改方向和单击按钮时,我都会调用 [tableView reloadData] 并且由于 dequeueReusableCellWithIdentifier:simpleTableIdentifier 找不到 simpleTableIdentifier,它总是会创建新单元格。如果我将 simpleIdentifier 定义为我的单元格 xib,它们在大小写更改(方向更改、展开状态更改)期间不会更改。

小区标识符的最佳用途是什么?我是否应该使用 dequeueReusableCellWithIdentifier:simpleTableIdentifier

以外的其他方法

我正在等待针对当前情况的任何解决方案,因为我确信一遍又一遍地加载每个单元格并不是最有效的方法。



Best Answer-推荐答案


这是一种方式,缓存 Nib

合成这个属性

@property (nonatomic, strong) id cellNib;

@synthesize cellNib = _cellNib;

自定义getter

-(id)cellNib
{
    if (!_cellNib) 
    {
        Class cls = NSClassFromString(@"UINib");
        if ([cls respondsToSelectorselector(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];
        }
    }

希望有帮助

关于ios - UITableViewCell 为不同的方向和单元格类型加载自定义 XIB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14960216/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4