ProductiPadCell
iPad用に設計されたという名前のカスタムセルクラスがあります。(iOS 5 SDK)
私が達成しようとしているのは、現在の状態(拡張されているかどうかに関係なく)とインターフェイスの向きに従ってカスタムセルをロードすることです。私はViewController内で両方の条件を処理していますが、問題はそのパフォーマンスが気に入らないことです。以下に定義されているxibsのいずれにもcellIdentifierを定義していませんが、最近、tableView
cellForRowAtIndexPath:
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)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:simpleTableIdentifier
simpleTableIdentifierが見つからないため、常に新しいセルが作成されます。セルxibを定義するsimpleIdentifier
と、ケースの変更(方向の変更、状態の変更の展開)中に変更されません。
セル識別子の最適な使用法は何ですか?他のアプローチを使用する必要がありますdequeueReusableCellWithIdentifier:simpleTableIdentifier
すべてのセルを何度もロードすることがこれを行う最も効率的な方法ではないと確信しているため、現在の状況に対する何らかの解決策を待っています。