2011-09-24 74 views
0

我已經成功地使用自定義UITableViewCells使用單獨的類,但我一直在想有一個更簡單的方法,所以我嘗試了以下。表格構建,但單元格是空白的(所以大概我的自定義單元格不能正確顯示)。任何想法讚賞。TableViewCell是否必須有自己的類?

我的文件有:

@interface RemediesPopUp : UIViewController <UITableViewDataSource, UITableViewDelegate>{ 

    UITableView *remediesDataTable; 
    UITableViewCell *remediesTableCell; // ** custom cell declared here ** 

    NSArray *remediesArray; 

} 

表格控件:

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

    static NSString *CellIdentifier = @"Cell"; 

    remediesTableCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (remediesTableCell == nil) { 
     remediesTableCell = [[[UITableViewCell alloc] init] autorelease]; 
    } 

    // Configure the cell. 

    return remediesTableCell; 

} 

我拖着一個自定義單元格對象到XIB文件窗格(所以我有視圖和一個單獨的自定義單元格顯示)。我將自定義單元連接到文件管理器。爲了測試,我將一些(靜態)標籤粘貼到自定義單元格上,以便看看是否正在加載。

頁面和表格加載得很好,但單元格是空白的(白色)。

任何想法?

回答

1

首先,確保您的自定義單元格是自定義子類(UITableViewCell的子類)並且具有自己的nib文件(例如,不要將其與您的表視圖控制器集成)。

在筆尖中,選擇身份檢查器並確保您的單元格的類是CustomCell(或任何您稱之爲的)。還要確保在屬性檢查器下,您將其Identifier設置爲「CustomCellIdentifier」或類似。

在您的表視圖的數據源,您tableView:cellForRowAtIndexPath方法應該包含類似的代碼如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier"; // Same as you set in your nib 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

     for (id oneCell in nib) { 
      if ([oneObject isKindOfClass:[CustomCell class]]) { 
       cell = (CustomCell *)oneCell; 
      } 
     } 
    } 

    // Customise the labels etc... 

    return cell; 
} 

這可以確保您的自定義單元格是從自己的筆尖直接加載,讓您直觀地佈置任何意見或標籤。這些標籤的文本可以在適當的單元出列/創建後設置。

在你的代碼中,你的'自定義'單元格只是一個普通的UITableViewCell。要使用界面構建器自定義單元格,您需要創建自己的自定義子類。

EDIT

對於自定義單元,其僅使用一個筆尖(和定製子類),改變上述代碼例如將細胞轉換爲普通UITableViewCell,即改變CustomCellUITableViewCell 。在Interface Builder中爲每個單元格的自定義視圖(標籤等)分配一個唯一的標籤。

有了到位,代碼應該是這個樣子:

#define MyLabelTag 10 // These should match the tags 
#define MyViewTag 11 // assigned in Interface Builder. 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier"; // Same as you set in your nib 

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

    for (id oneCell in nib) { 
     if ([oneObject isKindOfClass:[UITableViewCell class]]) { 
      cell = (UITableViewCell *)oneCell; 
     } 
    } 
} 

    // Get references to the views/labels from the tags. 
    UILabel *myLabel = (UILabel *)[cell viewWithTag:MyLabelTag]; 
    UIView *myView = (UIView *)[cell viewWithTag:MyViewTag]; 

    // Customise the views/labels... 
    // ... 

    return cell; 
} 
+0

+1,但不需要將單元格設置爲自定義子類。你的xib可以包含你喜歡的任何子視圖,你可以通過標籤來引用它們。 – jrturton

+0

@ jrturton:的確如此,儘管我個人認爲如果您可以通過出口而不是通過IB中指定的任意標籤來引用自定義單元格標籤,它會讓事情變得更加清晰。也許是個人喜好。 – Stuart

+0

謝謝你們。我認爲傑頓的想法更符合我的想法。我相信我不需要一個自定義的子類。有人可以用一些建議的代碼擴展這種想法。謝謝 – Jeremy

0

我通常所做的就是將UIView放入UITableViewCell中,然後將所有標籤和其他UI材質放在該UIView中。如果這不起作用,請告訴我。我可能有另一種解決方案。

+0

Mohabitar嗨。請參閱上面的評論。我試圖不使用子類,但我不知道如何引用自定義單元格中的對象。如果您有建議的代碼,將非常感謝。謝謝。 – Jeremy

相關問題