2

ProductiPadCelliPad用に設計されたという名前のカスタムセルクラスがあります。(iOS 5 SDK)

私が達成しようとしているのは、現在の状態(拡張されているかどうかに関係なく)とインターフェイスの向きに従ってカスタムセルをロードすることです。私はViewController内で両方の条件を処理していますが、問題はそのパフォーマンスが気に入らないことです。以下に定義されているxibsのいずれにもcellIdentifierを定義していませんが、最近tableViewcellForRowAtIndexPath:

ProductiPadCell4つの異なる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)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;
}

現在のアプローチ以外に、xibをロードするにはどうすればよいですか?向きを変えてボタンをクリックするたびに、私は呼び出しますが[tableView reloadData]dequeueReusableCellWithIdentifier:simpleTableIdentifiersimpleTableIdentifierが見つからないため、常に新しいセルが作成されます。セルxibを定義するsimpleIdentifierと、ケースの変更(方向の変更、状態の変更の展開)中に変更されません。

セル識別子の最適な使用法は何ですか?他のアプローチを使用する必要がありますdequeueReusableCellWithIdentifier:simpleTableIdentifier

すべてのセルを何度もロードすることがこれを行う最も効率的な方法ではないと確信しているため、現在の状況に対する何らかの解決策を待っています。

4

2 に答える 2

1

これは一方通行です、キャッシュ nib

この性質を合成する

@property (nonatomic, strong) id cellNib;

@synthesize cellNib = _cellNib;

カスタムゲッターを作る

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

    return _cellNib;
}

ビューでDidLoad

[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];
        }
    }

それが役立つことを願っています

于 2013-02-19T14:57:19.793 に答える
0

新しいメソッドregisterNib:forCellReuseIdentifier:を使用して、ペン先を登録する必要があります。これは、ペン先ごとに1回実行します。これは、viewDidLoadが適切な場所です。各ペン先のセルには、ペン先の登録に使用する再利用識別子と、デキューするときにcellForRowAtIndexPathにある必要があります。cellForRowAtIndexPathで使用する新しいメソッドは、dequeueReusableCellWithIdentifier:forIndexPath:です。その方法を使用すると、if(cell == nil)句を設定する必要はありません。必要に応じて、システムはnibから句を作成します。

于 2013-02-19T15:38:56.100 に答える