自定义UITableViewCell不调用prepareForSegue

我有一个名为EventCell的自定义UITableViewCell。

EventCell.h

 #import  @interface EventCell : UITableViewCell @property (nonatomic, strong) IBOutlet UILabel *titleLabel; @property (nonatomic, strong) IBOutlet UILabel *locationLabel; @property (nonatomic, strong) IBOutlet UILabel *dateLabel; @property (nonatomic, strong) IBOutlet UILabel *typeLabel; @end 

EventCell.m

 #import "EventCell.h" @implementation EventCell @synthesize titleLabel, locationLabel, dateLabel, typeLabel; - (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 } @end 

这是我如何设置我的单元格。

EventsMasterViewController.m

 - (EventCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { Event *myEvent; NSString *CellIdentifier = @"EventCell"; EventCell *cell = (EventCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EventCell" owner:nil options:nil]; for (id currentObject in topLevelObjects) { if ([currentObject isKindOfClass:[EventCell class]]) { cell = (EventCell *)currentObject; break; } } myEvent = [self.myEventsDataController objectInListAtIndex:indexPath.row]; cell.titleLabel.text = myEvent.name; cell.locationLabel.text = myEvent.location; cell.typeLabel.text = @"Social"; cell.layer.borderColor = [UIColor blackColor].CGColor; cell.layer.borderWidth = 1.0; return cell; } 

格式化的单元格很棒,看起来就像我需要的那样。 但是当我点击它时,单元格会突出显示蓝色并且不会转到下一个视图。 我在prepareForSegue方法中放了一个断点,它甚至没有被调用。

有没有办法手动调用prepareForSegue? 如果是这样,我应该在哪里做。

你需要实施

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:nil]; } 

你不应该为了这个而实现didSelectRow – 控制 – 从一个单元格拖动到另一个视图控制器应该创建一个在单击单元格时有效的segue。

就我而言,问题是故事板编辑器错误地创建了segue。 我有两个非常相似的故事板,一个工作,另一个没有,并查看源代码(右键单击storyboard文件并选择Open As – > Source Code ),我看到了:

  

请注意trigger属性。 这告诉我,segue将从细胞的辅助作用中发射出来。 您甚至可以从单元格的连接检查器中看到这个:

在此处输入图像描述

删除它并通过从上面的“选择”手柄拖动替换segue而不是从单元格中进行控制拖动,这给了我正确的segue。

我不确定这个特殊的单元是什么让控制拖动连接到附件动作而不是选择动作,但是你去了。

如上所述,除非将故事板中的segue配置为新的UIViewController否则需要使用didSelectRowAtIndexPath 。 这允许您使用prepareForSegue函数而不是performSegueWithIdentifier的编程调用。

希望有助于清理它!

我的问题是细胞标识符。

所以,在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath我声明了static NSString *cellIdentifier = @"cell"; 然后在storyBoard中我在这里添加了这个标识符:

在此处输入图像描述

你走吧!