2013-11-04 75 views
0

我有一個自定義單元格,我正在使用我的表格視圖加載一個類別列表,當選擇一個項目時,它應該帶你到下一個視圖列出該類別中的產品。塞格不使用自定義TableViewCell

問題是我在它自己的xib文件中創建了這個單元格,並創建了一個自定義類,當我嘗試將主要故事板中的原型單元連接到segue時,它不起作用。

如果我在didSelectCellForRowAtIndexPath中調用segue,它只會在您選擇另一個單元而非第一個單元時執行segue。但加載第一個單元格的信息,而不是第二個。

這是視圖控制器的一些代碼。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *qrTableIdentifier = @"QRTableCell"; 

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

    QRDoc *category = self.qrload.categories[[indexPath row]]; 

    cell.nameLabel.text = category.data.category; 
    cell.nameLabel.textColor = [UIColor performSelector:NSSelectorFromString(category.data.color)]; 
    cell.bulletImage.image = category.bullet; 
    return cell; 
} 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    NSLog(@"Preparing for seque"); 
    if ([[segue identifier] isEqualToString:@"showCategorySegue"]) { 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     //QRDoc *category = self.qrload.categories[[sender row]]; 
     QRDoc *category = self.qrload.categories[[indexPath row]]; 
     NSLog(@"Loading in new category"); 
     QRCatViewController *catVC = [segue destinationViewController]; 
     catVC.selectedCategory = category.data.category; 
     catVC.categoryColor = category.data.color; 
    } 
} 

而且對於實現代碼如下細胞,這是我認爲這個問題可能在於。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

細胞本身只由一個imageview的和現在的標籤,所以我不認爲這個問題可能是什麼吧。我是否需要以編程方式創建單元才能正常工作?

+3

你確定沒有在'didDeselectRowAtIndexPath'而不是'didSelectRowAtIndexPath'中調用segue嗎? – Larme

+0

我可能沒有,但我只是在它的代碼,它仍然無法正常工作。我認爲這可能與單元格的標識符有關。你有什麼想法嗎?我已經得到它與繼續工作,但單元格結束了空白。 –

+0

我不這麼認爲。把你試圖做的事粘​​貼到'didSelectRowAtIndexPath'中,因爲我們不知道你什麼時候調用'performSegue'。 – Larme

回答

0

很可能這只是一個錯字。

在這種情況下,我建議在開始洗涮,並且比較工作代碼沒有一個

enter image description here

我附上緊湊項目解決了自定義單元格與Storyboard,它提供了一種替代分離XIB

參考從分離XIB一個UITableViewCell僅需要一個單一的代碼行:

tableView.register(
    UINib(nibName: "XIBSectionHeader", bundle:nil), 
    forCellReuseIdentifier: "reuseIdentifier") 

►發現GitHub並將該溶液於Swift Recipes附加細節。