2012-06-14 40 views
3

我無法讓我的CustomTableViewCellUITableViewCell的子類出現在我的表格視圖中。使用XIB文件加載UITableViewCell子類

我使用xib來表示該單元格,但我假設數據源委託的代碼不會更改。我確保在表格視圖單元格XIB中設置一個相同的重用標識符。

我找出問題的事實,即返回表格單元格的數據源的方法不能正常工作,那就是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    DataObject *foo = [self.dataArray objectAtIndex:indexPath.row]; 

    if (cell == nil) 
    { 
     cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    [[cell overview] setText:foo.overview]; 
    [[cell price] setText:foo.price]; 
    NSLog(@"cell initialized, description text is %@",cell.overview.text); 

    return cell; 
} 

不知道爲什麼,這是行不通的,但最後的日誌語句總是在最後打印一個(null),而且我確實證實數據對象的overview屬性在其中有一個有效的字符串。 price同樣的事情。

+1

http://stackoverflow.com/questions/4195726/how-to -create-custom-tableviewcell-from-xib試試!!!我認爲你的答案在那裏! – trumpetlicks

+0

是的,它可以工作,但是當我在前幾次嘗試該方法時遇到了異常,請參閱我對如何避免接受的答案的評論。 –

+0

嘿@AndrewLauerBarinov,所以我在同一個問題atm,但我用故事板代替。想知道您對接受答案的評論,我似乎並不明白「IBOutlet與您所說的視圖不是文件所有者」的關係。對不起,我有點新與ios,所以我仍然試圖學習我需要做的連接和東西方面的​​東西。你能請進一步解釋一下嗎?謝謝!還在這裏發佈的東西http://stackoverflow.com/questions/15175472/custom-uitableviewcell-will-not-display-label-texts – gdubs

回答

1

在.h文件中,你寫它。

IBOulet CustomTableViewCell *tableCell; 

並連接到CustomTableViewCell的.xib文件中的文件所有者。

您修改這(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

if (cell == nil) 
{ 
    [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil]; 
    cell = tableCell; 
} 

我認爲這將是對你有所幫助。

+0

我想補充的一件事是確保你的IBOutlet連接是對視圖進行的,而不是文件的所有者(如果你這樣做,你會得到一個關鍵值相關的異常) –

+0

當我把一個屬性放在我的viewcontroller.h ...連接「」不能有一個原型對象作爲其目的地。 – gdubs

5

1)您的自定義單元格類單獨

2)廈門國際銀行:文件所有者類更改爲NSObject的

3)廈門國際銀行:UITableViewCell中更改爲要裏面添加MyTableViewCell

4)表視圖代碼作爲指數0,1

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

    static NSString *CellIdentifier = @"MyCell"; 

    MyTableViewCell *cell = (MyTableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     NSArray *arrayCellXib = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" 
                   owner:self 
                  options:nil]; 
     if (indexPath.row == 0) { 
      // first table cell 
      cell = [arrayCellXib objectAtIndex:0]; // * 
      ...... 
     }else { 

      cell = [arrayCellXib objectAtIndex:1]; // * 
     } 
     ... 
     } 
    return cell; 

} 

對象...是指數,其命令你做MyTableViewCell在廈門國際銀行,如果你想有更多的塔n一個自定義單元格。意味着在MyTableViewCell類中,您可以根據需要製作無限定製單元格和使用。

通常情況下,只要在廈門國際銀行ONE自定義單元格,並做到這一點:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    int thisRow = indexPath.row; 
    NSString *exampleText = [yourData objectAtIndex:thisRow]; 

    static NSString *CellID = @"cu5"; 

    FancyCell *cell = 
     (FancyCell *)[tableView dequeueReusableCellWithIdentifier:CellID]; 
    if (cell == nil) 
     { 
     NSArray *cellTeam = 
      [[NSBundle mainBundle] loadNibNamed:@"FancyCell" 
      owner:self options:nil]; 
     cell = [cellTeam objectAtIndex:0]; 
     } 

    [cell someFunction:exampleText]; 
    [cell.someLabel setText:exampleText]; 
    return cell; 
    } 

希望能可幫助ü:)

+0

這是一個真正有用的答案,非常感謝。這正是如何用CUSTOM XIB和CUSTOM CLASS來做到的。如果你想「JUST」使用定製的xib(不需要定製類),請使用Jamihash的答案:http://stackoverflow.com/questions/15378788/how-can-i-use-custom-uitableviewcell -and-UITableView的功能於同一廈門國際銀行 – Fattie