2015-12-23 42 views
1

起初,我有這樣的代碼:細胞預裝

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    DropDownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DropDownTableViewCell"]; 
    if (!cell) { 
     NSArray *topLevelObjects = 
     [[NSBundle mainBundle] loadNibNamed:@"DropDownView" 
             owner:self 
            options:nil]; 

     for (DropDownTableViewCell *object in topLevelObjects) { 
      if ([object class] == [DropDownTableViewCell class]) { 
       cell = object; 
       break; 
      } 
     } 
     NSAssert(cell, @"Cell must not be nil"); 
    } 

    cell.nameLabel.text = [self.dataSource buttonDownPicker:self stringForRowAtIndexPath:indexPath]; 

    return cell; 
} 

在第一時刻,當我表現出的tableView和的tableView細胞的開始從筆尖加載我有UI凍結幾秒鐘(由加載每個顯示單元的筆尖)。這可以通過從早期的筆尖加載細胞來解決:

- (void)awakeFromNib { 
    DropDownTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"DropDownView" owner:self options:nil] objectAtIndex:0];  
} 

但是這種方式看起來「哈克」。有更合適的解決方案嗎?

根據給出的答案編輯:

我試着使用registerNib forCellIdentifier但它並不會加載筆尖,它只是裝訂筆尖與標識,並在第一次時的tableView出現在所有細胞造成負荷筆尖到內存

回答

0

這根本不是「哈克」。對於從筆尖加載單元格,您通常所做的是加載筆尖,然後將單元格註冊爲您選擇的可重用標識符。例如,在我的一個項目中,我有一個帶有表格視圖的視圖控制器。在視圖做負載我打電話:

[[NSBundle mainBundle] loadNibNamed:@"ChannelListCell" owner:self options:nil]; 
[self.channelListTableView registerNib:[UINib nibWithNibName:@"ChannelListCell" bundle:nil] forCellReuseIdentifier:@"channelListCell"]; 

然後在行的單元格的索引路徑:

cell = [tableView dequeueReusableCellWithIdentifier:@"channelListCell" forIndexPath:indexPath]; 

在你的情況下,取出的細胞始終是零,因爲你沒有註冊。

0

嘗試這樣.....

- (void)viewDidLoad { 
     [super viewDidLoad]; 
     // Do any additional setup after loading the view, typically from a nib. 

     UINib *nib = [UINib nibWithNibName:@"" bundle:nil]; 
     [self.tblview registerNib:nib forCellReuseIdentifier:@"DropDownTableViewCell"]; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

     DropDownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DropDownTableViewCell"]; 


     return cell; 
    } 
+0

我還沒有viewDidLoad,我的課是一個UIView子。創建冗餘的筆尖鏈接與創建冗餘單元相同。我想避免它 –

0

你可以就像在你的viewDidLoad波紋管先註冊類。它做出更快,更好的

[self.tableViewObject registerClass: [DropDownTableViewCell class]forCellReuseIdentifier:@"DropDownTableViewCellIdentifier"]; 

並請在您的DropDownTableViewCell.m文件

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DropDownTableViewCell" owner:self options:nil]; 
     // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
     self = [topLevelObjects objectAtIndex:0]; 
    return self; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *[email protected]"DropDownTableViewCellIdentifier"; 
    DropDownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 

//Access all cell properties here. 

return cell. 
} 

請參考https://stackoverflow.com/a/30954273/914111更好的知識添加下面的方法。