自定义的UITableViewCell没有调用prepareForSegue方法

12

我有一个自定义的UITableViewCell,叫做EventCell

EventCell.h

#import <UIKit/UIKit.h>

@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方法?如果有,应该在哪里调用。


你的 tableView:didSelectRowAtIndexPath: 怎么样了? - Desdenova
我没有叫那个名字的方法,我需要实现它吗? - ardavis
根据我所阅读的,我们不是使用prepareForSegue代替didSelectRowAtIndexPath吗? - ardavis
4个回答

25

你需要实现

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {



[self performSegueWithIdentifier:@"YourSegueIdentifier" sender:nil];


}

1
请注意,didSelectRowAtIndexPath:和prepareForSegue:都是UIViewController的方法,而不是UITableViewCell的方法。 - Brabbeldas

4
您不需要实现didSelectRow就能让它工作——从单元格到另一个视图控制器的控件拖动应该创建一个在单击单元格时工作的segue。
在我的情况下,问题在于Storyboard编辑器错误地创建了segue。我有两个非常相似的Storyboard,一个正常工作,另一个则不能。查看源代码(右键单击Storyboard文件,选择打开方式->源代码),我看到了这个:
<segue destination="UWX-rF-KOc" kind="replace" identifier="showStory" trigger="accessoryAction" splitViewControllerTargetIndex="1" id="Gly-La-KIe"/>

请注意触发器属性。这表明,segue将从单元格的辅助操作中触发。您甚至可以从单元格的连接检查器中看到这一点:enter image description here 删除此并通过从上面的“选择”句柄拖动而不是从单元格控制拖动来替换segue,给了我正确的segue。
我不确定是什么原因使得控制拖动连接到辅助操作而不是选择操作,但是就是这样。

谢谢你的建议,我也遇到了同样的问题!! :) - Pangu

1
我的问题在于单元格标识符。因此,在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中,我声明了static NSString *cellIdentifier = @"cell";,然后在storyBoard中将此标识符添加到此处:

enter image description here

这是您要的内容!


1
如上所述,除非您在故事板中配置了一个到新的UIViewController的segue,否则您需要使用didSelectRowAtIndexPath。这使您可以使用prepareForSegue函数而不是编程调用performSegueWithIdentifier
希望这有助于澄清问题!

我认为 didSelectRowAtIndex 不是一个函数。请检查您的答案并更正函数名称(如果不正确)。 - Donovan
看起来我错过了结尾的路径。谢谢! - heckman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接