2012-08-11 113 views
12

我有一个自定义的UITableViewCell,名为EventCell自定义的UITableViewCell不能调用prepareForSegue

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

+0

你的'tableView:didSelectRowAtIndexPath:'? – Desdenova 2012-08-11 15:24:08

+0

我没有一种方法叫做,我需要实现吗? – ardavis 2012-08-11 15:30:37

+0

从我正在阅读的内容中,我们不使用'prepareForSegue'来代替'didSelectRowAtIndexPath'吗? – ardavis 2012-08-11 15:33:10

回答

25

您需要实现

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



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


} 
+1

请注意,didSelectRowAtIndexPath:和prepareForSegue:是UIViewController的方法,而不是UITableViewCell的方法 – Brabbeldas 2014-02-26 08:14:01

1

如上所述,你需要使用didSelectRowAtIndexPath除非你配置你的故事情节进行SEGUE到一个新的UIViewController。这允许您使用prepareForSegue功能而不是performSegueWithIdentifier的编程呼叫。

希望能帮助清理它!

+0

我不认为'didSelectRowAtIndex'是一个函数。如果不正确,请检查您的答案和正确的功能名称。 – Donovan 2013-06-12 06:38:04

+0

看起来像我错过了最后的道路。谢谢! – heckman 2013-10-26 13:05:35

1

我的问题是单元格标识符。

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

enter image description here

在这里,你去!

4

你不应该来实现didSelectRow这个工作 - 控制 - 从一个单元格拖动到另一个视图控制器应该创建一个segue,当点击单元格时工作。

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

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

注意trigger属性。这暗示了赛格将从单元的辅助动作中被解雇。你甚至可以看到从细胞的连接检查:

enter image description here

删除这一点,从“选择”拖动更换SEGUE处理上面,而不是控制拖动从小区的给了我正确的SEGUE 。

我不确定这是什么关于这个特定的单元格,使控制拖动连接到附件的行动,而不是选择行动,但你去。

+0

感谢您的建议,我有同样的问题! :) – Pangu 2015-01-22 07:55:17