我有一個名爲ProductiPadCell
的自定義單元類,專爲iPad設計。 (iOS 5 SDK)UITableViewCell爲不同的方向和單元類型加載自定義XIB
我想要實現的是根據當前狀態(擴展與否)和界面方向加載我的自定義單元格。我正在處理視圖控制器中的兩個條件,問題是我不喜歡它的性能。我已經NOT定義爲任何下面定義的xibs的cellIdentifier,最近我檢測到我的tableView
的cellForRowAtIndexPath:
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
我在等待任何解決方案,以我目前的情況,因爲我確定一遍又一遍地加載每個單元格不是這樣做的最有效的方式。
只是爲了確保我理解正確的話,我只是對我的情況下,如果在viewDidLoad中他們每個人都有不同的標識符和登記我的筆尖的所有4個(如你指出)4種不同的cellNib後,我檢查哪些筆尖我需要在tableView:cellForRowAtIndexPath:並調用dequeueReusableCellWithIdentifier:與該標識符?這是我第一次看到registerNib:forCellReuseIdentifier:並不常用。謝謝 ! – Bartu 2013-02-19 19:29:01