2014-01-22 51 views
0

我想要得到一個表來顯示名稱和排名列表。在每個單元格中,我想要有2個UILabel作爲子視圖 - 一個UILabel顯示排名,一個UILabel顯示名稱。Setter方法不工作的一個UITableViewCell子類的屬性

@interface MyTableViewCell : UITableViewCell 

@property UILabel* rankLabel; 
@property UILabel* nameLabel; 

我還沒有添加任何東西比MyTableView @synthesize各性能的其他的.M實現文件:

我如下創建一個UITableViewCell子類具有兩個屬性。實現文件的其餘部分正是xCode在創建UITableViewCell子類時爲您提供的內容。

然後,在包含該表的masterViewController,我有以下幾點:

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

    Person *object = _objects[indexPath.row]; 

    if (cell == nil) { 
    cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    cell.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.size.height, 0, cell.frame.size.width - cell.frame.size.height, cell.frame.size.height)]; 
    cell.nameLabel.text = object.name; 
    [cell addSubview:cell.nameLabel]; 

    NSString* rankString = [object.rank stringValue]; 
    cell.rankLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.height, cell.frame.size.height)]; 
    cell.rankLabel.text = rankString; 
    [cell addSubview:cell.rankLabel]; 
} 

當我在調試運行它,它會在那裏我設置cell.nameLabel或Alloc它的第一線,和拋出以下錯誤:

[UITableViewCell setNameLabel:]: unrecognized selector sent to instance 0x108fa64c0 

真的不明白爲什麼setNameLabel方法應該有問題在這裏。有任何想法嗎?提前致謝。

+0

你是不是在使用故事板?或者只是不爲這個特定的細胞? –

回答

3

該錯誤似乎告訴你確切的問題。這並不是說[MyTableViewCell setNameLabel:]它說:[UITableViewCell setNameLabel:]你可能已經忘了改類爲您的故事板MyTableViewCell細胞...

編輯:不,他不是用故事板,但顯然崩潰似乎有關細胞是錯誤的類。

編輯:是的,或者至少Xcode是爲他做的。

+0

我沒有爲這個程序使用故事板,但是感謝我將注意力集中在這個區別上......我現在回頭看看代碼,看看我是否能夠弄清楚爲什麼會出現這種差異。 – TheSneak

+0

乾杯!應該注意到你在代碼中執行它。這似乎很奇怪,因爲你明確地設置了標識符...... –

+0

不錯,你說得對。我以編程方式做所有事情,所以我認爲根本不必處理故事板,但XCode爲您提供的基本故事板需要將單元格類型轉換爲MyTableViewCell,就像您說的那樣。謝謝! – TheSneak

相關問題