2013-11-04 34 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附加细节。