自定义UITableViewCell不调用prepareForSegue

时间:2012-08-11 15:09:25

标签: ios uitableview segue

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

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?如果是这样,我应该在哪里做。

4 个答案:

答案 0 :(得分:25)

您需要实施

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



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


}

答案 1 :(得分:4)

你不应该 来实现didSelectRow来实现这一点 - 控制 - 从一个单元格拖动到另一个视图控制器应该创建一个在点击单元格时有效的segue。“ p>

就我而言,问题是故事板编辑器错误地创建了segue。我有两个非常相似的故事板,一个工作,另一个没有,并查看源代码(右键单击故事板文件并选择打开为 - &gt; 源代码),我看到了这个:

<segue destination="UWX-rF-KOc" kind="replace" identifier="showStory" trigger="accessoryAction" splitViewControllerTargetIndex="1" id="Gly-La-KIe"/>

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

enter image description here

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

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

答案 2 :(得分:1)

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

希望有助于清理它!

答案 3 :(得分:1)

我的问题是小区标识符。

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

enter image description here

你走了!

相关问题