2016-06-11 7 views
0

Verwenden Alex Di Mangos POPDownMenuTable-Master-Setup erweitert verschachtelte UITableviewCell und erfolgreich funktioniert gut in meinem Projekt ... Aber jetzt ist das Problem, wie zu anderen View-Controller aus dem navigieren Menü und verschachtelte/-erweiterung Untermenü UITableViewCells ... mit didSelectRowAtIndexPth wie alles programmatisch unter Code verweisen created..Please ...So navigieren Sie zu Viewcontrollern von erweiterten/verschachtelten UITableViewCells

**POPDCell.h** 

#import <UIKit/UIKit.h> 

@interface POPDCell : UITableViewCell 

@property (strong, nonatomic) IBOutlet UILabel *labelText; 
@property (strong, nonatomic) IBOutlet UILabel *separator; 
@property (strong, nonatomic) IBOutlet UILabel *sepShadow; 
@property (strong, nonatomic) IBOutlet UILabel *shadow; 

@end 

POPDCell.h

#import "POPDCell.h" 

@implementation POPDCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

-(void)prepareForReuse 
{ 
    self.backgroundColor = [UIColor clearColor]; 
} 
@end 

POPDSampleViewController.m 

#import "POPDSampleViewController.h" 
#import "POPDViewController.h" 

static NSString *kheader = @"menuSectionHeader"; 
static NSString *ksubSection = @"menuSubSection"; 

@interface POPDSampleViewController()<POPDDelegate> 

@end 

@implementation POPDSampleViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 



    NSArray *sucSectionsA = [NSArray arrayWithObjects:@"Personal Information",@"Emergency Contact", @"Family Physician", nil]; 
    NSArray *sucSectionsB = [NSArray arrayWithObjects:@"Current Medications",@"Medical Details", nil]; 
    NSArray *sucSectionsC = [NSArray arrayWithObjects:@"Appointments",@"Other Reminders", nil]; 
    NSArray *sucSectionsD = [NSArray arrayWithObjects:@"Health Risk Assessment",@"Short HRA", nil]; 


    NSDictionary *section1 = [NSDictionary dictionaryWithObjectsAndKeys: 
           @"Dashboard", kheader, nil]; 

    NSDictionary *sectionA = [NSDictionary dictionaryWithObjectsAndKeys: 
            @"My Profile", kheader, 
            sucSectionsA, ksubSection, 
            nil]; 

    NSDictionary *section2 = [NSDictionary dictionaryWithObjectsAndKeys: 
           @"My BMI", kheader, nil]; 

    NSDictionary *sectionB = [NSDictionary dictionaryWithObjectsAndKeys: 
           @"My Medical Status", kheader, 
           sucSectionsB, ksubSection, 
           nil]; 

    NSDictionary *sectionC = [NSDictionary dictionaryWithObjectsAndKeys: 
         @"My Health Planner", kheader, 
         sucSectionsC, ksubSection, 
         nil]; 
    NSDictionary *sectionD = [NSDictionary dictionaryWithObjectsAndKeys: 
           @"My Health Assessment", kheader, 
           sucSectionsD, ksubSection, 
           nil]; 
    NSDictionary *section3 = [NSDictionary dictionaryWithObjectsAndKeys: 
           @"My Wellness Tracker", kheader, nil]; 


    NSArray *menu = [NSArray arrayWithObjects: section1, sectionA, section2, sectionB, sectionC, sectionD, section3, nil]; 
    POPDViewController *popMenu = [[POPDViewController alloc]initWithMenuSections:menu]; 
    popMenu.delegate = self; 
    popMenu.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 
    //ios7 status bar 
//  popMenu.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0); 

    [self addChildViewController:popMenu]; 
    [self.view addSubview:popMenu.view]; 

} 

#pragma mark POPDViewController delegate 

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

    NSLog(@"didSelectRowAtIndexPath: %ld,%ld",(long)indexPath.section,(long)indexPath.row); 

} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

**POPDViewController.h** 

#import <UIKit/UIKit.h> 
#ifndef MB_STRONG 
#if __has_feature(objc_arc) 
#define MB_STRONG strong 
#else 
#define MB_STRONG retain 
#endif 
#endif 

#ifndef MB_WEAK 
#if __has_feature(objc_arc_weak) 
#define MB_WEAK weak 
#elif __has_feature(objc_arc) 
#define MB_WEAK unsafe_unretained 
#else 
#define MB_WEAK assign 
#endif 
#endif 

@protocol POPDDelegate <NSObject> 

-(void) didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 

@end 


@interface POPDViewController : UITableViewController 
- (id)initWithMenuSections:(NSArray *) menuSections; 
@property (MB_WEAK) id<POPDDelegate> delegate; 

@end 

**POPDViewController.m** 

#import "POPDViewController.h" 
#import "POPDCell.h" 
#import "TableViewController.h" 
#import "POPDSampleViewController.h" 



#define TABLECOLOR [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0] 
#define CELLSELECTED [UIColor colorWithRed:4.0/255.0 green:198.0/255.0 blue:251.0/255.0 alpha:1.0] 
#define SEPARATOR [UIColor colorWithRed:124.0/255.0 green:130.0/255.0 blue:131.0/255.0 alpha:1.0] 
#define SEPSHADOW [UIColor colorWithRed:80.0/255.0 green:97.0/255.0 blue:110.0/255.0 alpha:1.0] 
#define SHADOW [UIColor colorWithRed:69.0/255.0 green:84.0/255.0 blue:95.0/255.0 alpha:1.0] 
#define TEXT [UIColor colorWithRed:24.0/255.0 green:22.0/255.0 blue:121.0/255.0 alpha:1.0] 

static NSString *kheader = @"menuSectionHeader"; 
static NSString *ksubSection = @"menuSubSection"; 

@interface POPDViewController() 
@property NSArray *sections; 
@property (strong, nonatomic) NSMutableArray *sectionsArray; 
@property (strong, nonatomic) NSMutableArray *showingArray; 
@end 


@implementation POPDViewController 
@synthesize delegate; 

- (id)initWithMenuSections:(NSArray *) menuSections 
{ 
    self = [super init]; 
    if (self) { 
     self.sections = menuSections; 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0); 
    self.tableView.contentInset = inset; 


// [[UITableView appearance] setSeparatorColor:[UIColor grayColor]]; 


    self.tableView.tableHeaderView = ({ 
     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 80.0f)]; 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(-50, 0, 200, 80)]; 
     imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
     imageView.image = [UIImage imageNamed:@"logo-hd.png"]; 
     imageView.layer.masksToBounds = YES; 
     //  imageView.layer.cornerRadius = 50.0; 
     imageView.layer.borderColor = [UIColor clearColor].CGColor; 
     imageView.layer.borderWidth = 1.0f; 
     imageView.layer.rasterizationScale = [UIScreen mainScreen].scale; 
     imageView.layer.shouldRasterize = YES; 
     imageView.clipsToBounds = YES; 

     [view addSubview:imageView]; 
     view; 
    }); 


    self.tableView.backgroundColor = TABLECOLOR; 
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine]; 

    self.tableView.frame = self.view.frame; 

    self.sectionsArray = [NSMutableArray new]; 
    self.showingArray = [NSMutableArray new]; 
    [self setMenuSections:self.sections]; 

} 

- (void)setMenuSections:(NSArray *)menuSections{ 

    for (NSDictionary *sec in menuSections) { 

     NSString *header = [sec objectForKey:kheader]; 
     NSArray *subSection = [sec objectForKey:ksubSection]; 

     NSMutableArray *section = [NSMutableArray new]; 
     [section addObject:header]; 

     for (NSString *sub in subSection) { 
      [section addObject:sub]; 
     } 
     [self.sectionsArray addObject:section]; 
     [self.showingArray addObject:[NSNumber numberWithBool:NO]]; 
    } 

    [self.tableView reloadData]; 
} 



- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{  
    return [self.sectionsArray count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{  
    if (![[self.showingArray objectAtIndex:section]boolValue]) { 

     return 1; 
    } 
    else{ 

     return [[self.sectionsArray objectAtIndex:section]count];; 
    } 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if(indexPath.row ==0){ 
    if([[self.showingArray objectAtIndex:indexPath.section]boolValue]){ 
     [cell setBackgroundColor:CELLSELECTED]; 
    }else{ 
     [cell setBackgroundColor:[UIColor clearColor]]; 
    } 
    } 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"menuCell"; 
    #warning : Use here your custom cell, instead of POPDCell 

    POPDCell *cell = nil; 
    cell = (POPDCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"POPDCell" owner:self options:nil]; 

    if (cell == nil) { 
     cell = [topLevelObjects objectAtIndex:0]; 
    } 

    cell.labelText.text = [[self.sectionsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
    cell.labelText.textColor = TEXT; 
    cell.separator.backgroundColor = SEPARATOR; 
    cell.sepShadow.backgroundColor = SEPSHADOW; 
    cell.shadow.backgroundColor = SHADOW; 

// cell.layer.borderWidth = 0.5; 

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    return cell; 
} 

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

    if([[self.showingArray objectAtIndex:indexPath.section]boolValue]){ 

     [self.showingArray setObject:[NSNumber numberWithBool:NO] atIndexedSubscript:indexPath.section]; 
    }else{ 
     [self.showingArray setObject:[NSNumber numberWithBool:YES] atIndexedSubscript:indexPath.section]; 
    } 
    [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade]; 

    [self.delegate didSelectRowAtIndexPath:indexPath]; 
} 


@end 

Antwort

1

In DidSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    if (indexPath.row ==0) { 
     [self performSegueWithIdentifier:@"SegueIdentifier0" sender:self]; 
    }else if(indexPath.row==1{ 
     [self performSegueWithIdentifier:@"SegueIdentifier1" sender:self]; 
    }//and so on 
} 

je nachdem, wie viele s Egues du hast, könnte das weitergehen. Im Storyboard müssen Sie die Anzahl der Prototypzellen erhöhen, indem Sie bei der Auswahl von tableView zum Attributinspektor gehen. Sie müssen auch control-click-drag auf die ViewControllers, mit denen Sie segmentieren möchten, auswählen und drücken. Dann nennen Sie die Identifier „SegueIdentifer0“ ...

dann brauchen Sie eine prepareForSeque Methode

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

if ([segue.identifier isEqualToString:@"SegueIdentifier0"]) { 
    NSLog(@"Segue1"); 
    }else if ([segue.identifier isEqualToString:@"SegueIdentifier1"]){ 
     NSLog(@"Segue2"); 
    } 
} 
+0

Danke Kyle .... Ich werde diese Methode versuchen und das Ergebnis posten ... –

+0

wie ich keine tabueviecontroller in Storyboard verwendet habe ... konnte ich nicht mit segue verbinden .. ich werde versuchen mit orxelm's Methode.... –

0

Sie nicht segues zu verwenden sind verpflichtet, können Sie immer Ihre Viewcontrollers von Code initialisiert werden und dann präsentieren sie:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    NSString *identifier = @""; 

    if (indexPath.row ==0) { 
     identifier = @"VC1"; 
    } 
    else if(indexPath.row==1) { 
     identifier = @"VC2"; 
    } 

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:identifier]; 
    [self.navigationController showViewController:viewController sender:NULL]; 
} 
Verwandte Themen