2011-08-22 124 views
3

我正在構建一個應用程序,我在其中使用自定義tableView單元格,而且我不太確定,如果我正確地做到這一點: 我並不確定cellOwner的例子......custom uitableviewCell outlets outlets return nil

而基本的問題是,我在IB定義和打包的所有網點在運行時都是零。 這裏是我做過什麼:

  • 打造的UITableViewCell的自定義子類(ProductListTableViewCell)
  • 做了筆尖,包含一個UITableViewCell的根元素
  • 同時設置的className和業主類型細胞對ProductListTableViewCell
  • 通過拖放設置重用標識符
  • 然後把一對夫婦標籤等等到電池和已生成的出口和降
  • 雙重檢查噸帽子所有的插座都正確地掛起。

然而,零售點都是零。 我在做什麼錯?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *cellIdentifier = @"ProductListTableViewCell"; 

ProductListTableViewCell *cell =(ProductListTableViewCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell == nil) { 
    ProductListTableViewCell *cellOwner = [[ProductListTableViewCell alloc]init]; 

    UINib* nib = [UINib nibWithNibName:@"ProductListTableViewCell" bundle:nil]; 

    cell =(ProductListTableViewCell*) [[nib instantiateWithOwner:cellOwner options:nil]objectAtIndex:0]; 

} 
// all outlets are nil here 
+0

你相同的標識符ProductListTableViewCell設爲您的表視圖XIB? –

+0

是 - 相同的標識符通過IB – HeikoG

回答

0

試試這個

static NSString *CellIdentifier = @"ProductListTableViewCell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"ProductListTableViewCell" owner:self options:nil]; 
    cell = ProductListTableViewCell; 
    self.ProductListTableViewCell=nil; 

} 
+0

確定。我試圖通過NSBundle加載它 - 但是失敗了,因爲我作爲所有者傳入nil ... – HeikoG

+0

將所有者設置爲自定義表格單元格的另一個實例與通過UINib實現的效果相同 - 所有Outlet都是零。 – HeikoG

+0

好 - 我無法弄清楚,所以我去'cell = [[[NSBundle mainBundle] loadNibNamed:@「TableCellBackgroundView」owner:self options:nil] lastObject];'因爲我無法獲得對網點的引用我通過'[((UIButton *)[cell viewWithTag:100])將它們配置爲setImage:[UIImage imageNamed:@「Default-Landscape〜ipad」] forState:UIControlStateNormal];' – HeikoG

相關問題