ios - UITableViewCell 为不同的方向和单元格类型加载自定义 XIB
<p><p>我有一个名为 <code>ProductiPadCell</code> 的自定义单元格类,专为 iPad 设计。 (iOS 5 SDK)</p>
<p>我想要实现的是根据当前状态(扩展与否)和界面方向加载我的自定义单元格。我在 ViewController 中处理这两种情况,问题是我不喜欢它的性能。我<strong>NOT</strong>为下面定义的任何xibs定义了一个cellIdentifier,最近我检测到我的<code>tableView</code>的<code>cellForRowAtIndexPath:</code></p>
<p><code>ProductiPadCell</code> 有 4 个不同的 XIB;</p>
<pre><code>ProductiPadCell-Regular.xib
ProductiPadCell-Regular-Landscape.xib
ProductiPadCell-Expanded.xib
ProductiPadCell-Expanded-Landscape.xib
</code></pre>
<p>ProductiPadCell 定义;</p>
<pre><code>@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
</code></pre>
<p>TableView 的 cellForForAtIndexPath</p>
<pre><code>- (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 = [[ objectAtIndex:indexPath.row] boolValue];
ProductiPadCell *cell = (ProductiPadCell *);
if (cell == nil)
{
// Load the required nib for the current orientation and detail selection style
if (deviceOrientation == UIDeviceOrientationPortrait) {
if (isExpanded) {
NSArray *nib = [ loadNibNamed:@"ProductiPadCell-Regular-Expanded" owner:self options:nil];
cell = ;
// additional settings for expanded case
}
else {
NSArray *nib = [ loadNibNamed:@"ProductiPadCell-Regular" owner:self options:nil];
cell = ;
// additional settings for NOT expanded case
}
}
else {
if (isExpanded) {
NSArray *nib = [ loadNibNamed:@"ProductiPadCell-Landscape-Expanded" owner:self options:nil];
cell = ;
}
else {
NSArray *nib = [ loadNibNamed:@"ProductiPadCell-Regular-Landscape" owner:self options:nil];
cell = ;
}
}
}
// connect cell information with cell IBOutlets
cell.productDescriptionLabel.text = [ objectAtIndex:indexPath.row];
// ....
];
;
;
.CGColor];
;
if (isExpanded) {
cell.detailLabel.text = [ objectAtIndex:indexPath.row];
}
return cell;
}
</code></pre>
<p>除了我目前的方法之外,我应该怎么做才能加载我的 xib?在每次更改方向和单击按钮时,我都会调用 <code></code> 并且由于 <code>dequeueReusableCellWithIdentifier:simpleTableIdentifier</code> 找不到 simpleTableIdentifier,它总是会创建新单元格。如果我将 <code>simpleIdentifier</code> 定义为我的单元格 xib,它们在大小写更改(方向更改、展开状态更改)期间不会更改。</p>
<p>小区标识符的最佳用途是什么?我是否应该使用 <code>dequeueReusableCellWithIdentifier:simpleTableIdentifier</code></p> 以外的其他方法
<p>我正在等待针对当前情况的任何解决方案,因为我确信一遍又一遍地加载每个单元格并不是最有效的方法。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这是一种方式,缓存 Nib </p>
<p>合成这个属性</p>
<pre><code>@property (nonatomic, strong) id cellNib;
@synthesize cellNib = _cellNib;
</code></pre>
<p>自定义getter</p>
<pre><code>-(id)cellNib
{
if (!_cellNib)
{
Class cls = NSClassFromString(@"UINib");
if ()
{
_cellNib = [] retain];
}
}
return _cellNib;
}
</code></pre>
<p>在 <strong>viewDidLoad</strong></p>
<pre><code> forCellReuseIdentifier:kCellIdentification];
</code></pre>
<p>在 <strong>tableView:cellForRowAtIndexPath:</strong></p>
<pre><code>SomeCell *cell = ;
if (cell == nil)
{
//If nib cache
if () {
NSArray* viewFromNib = [ instantiateWithOwner:self options:nil];
cell = (SomeCell *);
}else { //nib not cache
NSArray *views = [ loadNibNamed:@"SomeCell" owner:nil options:nil];
cell = (SomeCell *);
}
}
</code></pre>
<p>希望有帮助</p></p>
<p style="font-size: 20px;">关于ios - UITableViewCell 为不同的方向和单元格类型加载自定义 XIB,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/14960216/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/14960216/
</a>
</p>
页:
[1]