2012-11-23 45 views
2

我有一個擴展UITableViewCell的自定義類。它有兩個標籤和一個UISegmentedControl。爲什麼我的自定義UITableViewCell永不改變原型?

這是我配置的cellForRowAtIndexPath()。當我在調試器中檢查「單元」時,它具有我提供的所有數據。但不知何故數據從未得到應用。目前

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

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

    MyData *my_data = [rows objectAtIndex:indexPath.row]; 

    UILabel *my_date = [[UILabel alloc] init]; 
    my_date.text = my_data.myDate; 
    [cell setMyDateLabel:my_date]; 

    UILabel *my_question = [[UILabel alloc] init]; 
    my_question.text = my.question; 
    [cell setMyQuestionLabel:my_question]; 


    UISegmentedControl *my_choices = [[UISegmentedControl alloc] 
             initWithItems:[NSArray arrayWithObjects:my.firstChoice, my.secondChoice, nil]]; 
    [my_choices setSelectedSegmentIndex:my.choice]; 
    [cell setMyChoiceSegments:my_choices]; 

    return cell 
} 

,我想顯示的數據是在一個數組我viewDidLoad中(創建),其通過「行」 var爲至的cellForRowAtIndexPath()訪問。

當我在模擬器中運行代碼時,我在表中得到了三行,代表我在viewDidLoad()中創建的數組中的三個元素。但是,這些行的內容與我在故事板中定義的完全一樣。

我錯過了什麼?

回答

2
  1. 你在哪裏定義你的細胞佈局?在NIB?在你的故事板?以編程方式在您的initWithStyleCustomGameCell?實現細節根據您使用的方法而有所不同,但您絕對需要在故事板中定義NIB或原型單元格,或以編程方式創建控件,設置框架,執行addSubview以使它們包含在單元格中,等等

  2. 您的代碼添加了新的UILabel對象,不會將它們作爲子視圖添加到任何對象中,無論您是否使用已出隊的單元格等,都會這樣做。要查看如何正確使用自定義單元格的示例,請參閱表視圖編程指南中的Customizing Cells但是,就像我說的,細節因您設計子類型UITableViewCell佈局而有所不同,所以我毫不猶豫地提出任何代碼,直到您指定如何設計用戶界面。

2

您必須在單元格的單元格內容視圖中添加了標籤和segmentcontrol,否則請這樣做。

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

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

    MyData *my_data = [rows objectAtIndex:indexPath.row]; 

    cell.myDateLabel.text = my_data.myDate; 

    cell.myQuestionLabel.text = my.question; 

    [cell.myChoiceSegments setSelectedSegmentIndex:my.choice]; 

    [cell autorelease]; 
    return cell 
} 

同樣使用autorelease進行存儲器管理。

+0

我會試試看。但是,我需要使用autorelease與ARC? –

+1

@DarrellBrogdon你沒有。 Shekhar顯然認爲你沒有使用ARC。 (我不知道爲什麼任何人現在都不會只使用ARC!) – Rob

相關問題