使用 Alex Di Mango 的 POPDownMenuTable-master 设置扩展嵌套的 UITableviewCell 并在我的项目中成功运行......但现在的问题是如何从菜单导航到其他 View Controller 并嵌套/扩展子菜单 UITableviewcells...使用 didSelectRowAtIndexPth 作为以编程方式创建的所有内容...请引用下面的代码...
**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
最佳答案
在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
}
取决于您有多少可以继续的 segues。在 Storyboard 中,您需要在选择 tableView 时转到属性检查器来增加原型(prototype)单元格的数量。您还需要按住 control-click-drag 到您希望使用的 viewControllers 并选择 push。然后将这些标识符命名为“SegueIdentifer0”...
那么你需要一个prepareForSeque方法
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"SegueIdentifier0"]) {
NSLog(@"Segue1");
}else if ([segue.identifier isEqualToString:@"SegueIdentifier1"]){
NSLog(@"Segue2");
}
}
关于ios - 如何从扩展/嵌套的 UITableViewCell 导航到 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37759835/