2012-08-11 111 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