2012-04-24 59 views
1

嗨我试图推动到一个不同的视图,取决于用户选择。我有一个桌面视图,其中有4个其他视图控制器通过故事板中的推段连接到它。所有Segues都直接链接到TableView视图控制器。根据选择的单元格,它会加载相应的视图。继承人我正在使用的代码:performSegueWithIdentifier是不工作的所有

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


    NSDictionary *dictionary = [_articles objectAtIndex:indexPath.row]; 
    NSArray *selectedKey = [dictionary objectForKey:@"Key"]; 


    if ([selectedKey isEqual:@"Driver"]){ 

     self.driverDetailView.wikiItem = dictionary; 
     [self.driverDetailView performSegueWithIdentifier:@"pushDriver" sender:dictionary]; 
     NSLog(@"Push Driver"); 
    } 

    if ([selectedKey isEqual:@"Team"]) { 
     NSLog(@"Push Team"); 
    } 
} 


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    NSLog(@"Preparing For Segue"); 
    if ([[segue identifier] isEqual:@"pushDriver"]) { 
     self.driverDetailView=segue.destinationViewController; 
    } 

} 

出于某种原因

[self.driverDetailView performSegueWithIdentifier:@"pushDriver" sender:dictionary]; 
NSLog(@"Push Driver"); 

不会让我将其设置为“自我”刚开始时,我得到一个错误说:

为TableCell的不可见的@interface声明选择 performSegueWithIdentifier

如果我使用“self.driverDetailView”,我不会收到错误,但是当我选择单元格时没有任何反应。

赫雷什Horizo​​ntalTableView.h

#import <UIKit/UIKit.h> 
#import "HorizontalDetailView.h" 
#import "HorizontalTableCell.h" 
@interface HorizontalTableView : UITableViewController <UITableViewDelegate, UITableViewDataSource> { 

NSDictionary *_articleDictionary; 
NSMutableArray *_reusableCells; 

} 

@property (nonatomic, retain) NSDictionary *articleDictionary; 
@property (nonatomic, retain) NSMutableArray *reusableCells; 


@end 

赫雷什Horizo​​ntalTableView.m

#import "HorizontalTableView.h" 
#import "HorizontalTableCell.h" 
#import "ControlVariables.h" 
#import "HorizontalDetailView.h" 

#define kHeadlineSectionHeight 26 
#define kRegularSectionHeight 18 

@interface HorizontalTableView() 

@end 

@implementation HorizontalTableView 
@synthesize articleDictionary = _articleDictionary; 
@synthesize reusableCells = _reusableCells; 



- (void)awakeFromNib{ 
    [self.tableView setBackgroundColor:kVerticalTableBackgroundColor]; 
    self.tableView.rowHeight = kCellHeight + (kRowVerticalPadding * 0.5) + ((kRowVerticalPadding * 0.5) * 0.5); 
} 

- (id)initWithStyle:(UITableViewStyle)style{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad{ 
    [super viewDidLoad]; 

    self.articleDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]]; 
} 

- (void)viewDidUnload{ 
    [super viewDidUnload]; 

    self.articleDictionary = nil; 
    self.reusableCells = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:  (UIInterfaceOrientation)interfaceOrientation{ 

    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    return [self.articleDictionary.allKeys count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    return 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"HorizontalCell"; 

    HorizontalTableCell *cell = (HorizontalTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil){ 
     cell = [[HorizontalTableCell alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)]; 
    } 

    NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)]; 
    NSArray* sortedCategories = [self.articleDictionary.allKeys sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 

    NSString *categoryName = [sortedCategories objectAtIndex:indexPath.section]; 

    NSArray *currentCategory = [self.articleDictionary objectForKey:categoryName]; 

    cell.articles = currentCategory; 

    return cell; 
} 


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 

    return section == 0 ? kHeadlineSectionHeight : kRegularSectionHeight; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 

    UIView *customSectionHeaderView; 
    UILabel *titleLabel; 
    UIFont *labelFont; 

    if (section == 0) 
    { 
     customSectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, kHeadlineSectionHeight)]; 

     titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, tableView.frame.size.width, kHeadlineSectionHeight)]; 
     labelFont = [UIFont boldSystemFontOfSize:20]; 
    } 
    else 
    { 
     customSectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, kRegularSectionHeight)]; 

     titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, tableView.frame.size.width, kRegularSectionHeight)]; 

     labelFont = [UIFont boldSystemFontOfSize:14]; 
    } 

    customSectionHeaderView.backgroundColor = [UIColor colorWithRed:0.01176471 green:0.01176471 blue:0.01960784 alpha:0.95]; 

    titleLabel.textAlignment = UITextAlignmentLeft; 
    [titleLabel setTextColor:[UIColor whiteColor]]; 
    [titleLabel setBackgroundColor:[UIColor clearColor]]; 
    titleLabel.font = labelFont; 

    NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)]; 
    NSArray* sortedCategories = [self.articleDictionary.allKeys sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 

    NSString *categoryName = [sortedCategories objectAtIndex:section]; 

    titleLabel.text = [categoryName substringFromIndex:1]; 

    [customSectionHeaderView addSubview:titleLabel]; 


    return customSectionHeaderView; 
} 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    NSLog(@"Preparing For Segue"); 
} 

@end 

的代码Horizo​​ntalTableCell.h是:

#import <UIKit/UIKit.h> 
#import "HorizontalDetailView.h" 
#import "DriverDetailView.h" 
#import "HorizontalTableView.h" 
@interface HorizontalTableCell : UITableViewCell <UITableViewDelegate, UITableViewDataSource> { 

    UITableView *_horizontalTableView; 
    NSMutableArray *_articles; 
    HorizontalTableCell *horizontalTableCell; 

} 


@property (nonatomic, strong) UITableView *horizontalTableView; 
@property (nonatomic, strong) NSMutableArray *articles; 
@property (nonatomic, strong) HorizontalDetailView *horizontalDetailView; 
@property (nonatomic, strong) DriverDetailView *driverDetailView; 
@property (nonatomic, strong) HorizontalTableCell *horizontalTableCell; 
@end 

为Horizo​​ntalTableCell.m的代码是:

#import "HorizontalTableCell.h" 
#import "ControlVariables.h" 
#import "ArticleCell.h" 
#import "HorizontalDetailView.h" 
@implementation HorizontalTableCell 

@synthesize horizontalTableView = _horizontalTableView; 
@synthesize articles = _articles; 
@synthesize horizontalDetailView; 
@synthesize driverDetailView; 
@synthesize horizontalTableCell; 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [self.articles count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"Cell"; 

    ArticleCell *cell = (ArticleCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil){ 
     cell = [[ArticleCell alloc] initWithFrame:CGRectMake(0, 0, kCellWidth, kCellHeight)]; 
    } 

    NSDictionary *currentArticle = [self.articles objectAtIndex:indexPath.row]; 

    cell.thumbnail.image = [UIImage imageNamed:[currentArticle objectForKey:@"Image"]]; 
    cell.titleLabel.text = [currentArticle objectForKey:@"Title"]; 

    return cell; 
} 

- (void)dealloc{ 
    self.horizontalTableView = nil; 
    self.articles = nil; 
} 

- (NSString *) reuseIdentifier{ 
    return @"HorizontalCell"; 
} 

- (id)initWithFrame:(CGRect)frame{ 
    if ((self = [super initWithFrame:frame])){ 
     self.horizontalTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kCellHeight, kTableLength)]; 
     self.horizontalTableView.showsVerticalScrollIndicator = NO; 
     self.horizontalTableView.showsHorizontalScrollIndicator = NO; 
     self.horizontalTableView.transform = CGAffineTransformMakeRotation(-M_PI * 0.5); 
     [self.horizontalTableView setFrame:CGRectMake(kRowHorizontalPadding * 0.5, kRowVerticalPadding * 
                 0.5, kTableLength - kRowHorizontalPadding, kCellHeight)]; 

     self.horizontalTableView.rowHeight = kCellWidth; 
     self.horizontalTableView.backgroundColor = kHorizontalTableBackgroundColor; 

     self.horizontalTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
     self.horizontalTableView.separatorColor = [UIColor clearColor]; 

     self.horizontalTableView.dataSource = self; 
     self.horizontalTableView.delegate = self; 
     [self addSubview:self.horizontalTableView]; 
    } 

    return self; 
} 

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

    NSDictionary *dictionary = [_articles objectAtIndex:indexPath.row]; 
    NSArray *selectedKey = [dictionary objectForKey:@"Key"]; 
    // NSLog(@"Selected Key = %@",selectedKey); 

    if ([selectedKey isEqual:@"Driver"]){ 

     self.driverDetailView.wikiItem = dictionary; 
     [self performSegueWithIdentifier:@"pushDriver" sender:self]; 
     NSLog(@"Push Driver"); 
    } 

    if ([selectedKey isEqual:@"Team"]) { 
     NSLog(@"Push Team"); 
    } 

    if ([selectedKey isEqual:@"Tech"]) { 
     NSLog(@"Push Tech"); 
    } 

    if ([selectedKey isEqual:@"Track"]) { 
     NSLog(@"Push Track"); 
    } 

    //self.horizontalDetailView.wikiItem = dictionary; 

    // NSLog(@"selected Array = %@",dictionary); 

} 


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    NSLog(@"Preparing For Segue"); 
    if ([[segue identifier] isEqual:@"pushDriver"]) { 
     self.driverDetailView=segue.destinationViewController; 
    } 
} 

@end 

回答

0

如果我正确理解你,你有你的segues挂钩到表格视图单元格。如果这是正确的,我建议将它们从视图控制器本身挂接到目标视图控制器,然后在didSelectRowAtIndexPath:方法中调用[self performSegueWithIdentifier:@"pushDriver"];

+0

感谢您的回复,我已连接到视图控制器的所有4个segues,以及上面的代码已在didSelectRowatIndexPath实现,当我选择单元没有任何反应。我不能将它设置为[self performSegueWithIdentifier:@“pushDriver”],因为我得到一个错误。我必须将其设置为[self.driverDetailView performSegueWithIdentifier:@“pushDriver”] – 2012-04-24 21:38:56

+0

哦,我的错是因为没有仔细阅读你的代码。 driverDetailView是实现这些方法的类的一个实例吗?从错误消息中看起来像是在您的UITableViewCell的子类上调用performSegueWithIdentifier:方法。 – geraldWilliam 2012-04-24 21:51:24

+0

是的,它被称为他们的,因为那didslectRowAtIndex方法是。我试图把didSelectRowAtIndex方法放在UITableView的子类中,但它不起作用。该表是一个水平滚动表,在自定义单元格内有一个自定义单元格,只是为了让事情更复杂一点! – 2012-04-25 17:11:59