2014-07-01 23 views
-2

我下面this教程創建一個自定義的UITableViewCell,並在對象從返回從[tableView dequeueReusableCellWithIdentifier:@"ID"],所有的UILabels和等都是nil。本教程不說有關創建他們自己什麼,我希望我不會需要,內部監督辦公室的其餘部分的工作方式來看,但我不知道爲什麼沒有被自動創建他們。例如,這裏cell.lightNamenil,即使它掛接到離開,在IB的自定義視圖的標籤。我必須創建所有的UI元素自己使用自定義的UITableViewCell什麼時候?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *dataCellIdentifier = @"Data Cell"; 
    AnalysisHistoryTableViewCell *cell = (AnalysisHistoryTableViewCell*)[tableView dequeueReusableCellWithIdentifier:dataCellIdentifier forIndexPath:indexPath];  
    self.selectedAnalysis = self.analysisResults[indexPath.row]; 
    cell.lightNameLabel.text = self.selectedAnalysis.lightName; 
    return cell; 
} 

我在做什麼錯誤導致我的UILabel s不存在?

+0

我假設你AnalysisHistoryTableViewCell已合成的屬性,連接到您的手機中的UILabel。 – userx

+2

你有登記表視圖('registerNib:')筆尖? – sooper

+0

你使用Xib還是故事板?你在哪裏宣佈標籤?你是否合成了標籤? – vnchopra

回答

1

通過自定義單元格,您需要註冊筆尖第一:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self.tableView registerNib:[UINib nibWithNibName:@"AnalysisHistoryTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"Data Cell"]; 
} 

然後在您的cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    AnalysisHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Data Cell"]; 

    //etc 

    return cell; 
} 
相關問題