我在表视图中使用的自定义单元格加载了类别列表,并且在选择一个项目时,它应该带您进入下一个视图,该视图列出了该类别中的产品。
问题是我已经在自己的 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;
}
}
而对于 tableview 单元格,我认为问题可能出在哪里。
- (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
}
单元格本身现在只包含一个图像视图和标签,所以我看不出它可能有什么问题。我需要以编程方式创建单元格才能正常工作吗?