ios-使用树形结构导航模式
来源:互联网 发布:农产品追溯系统源码 编辑:程序博客网 时间:2024/06/05 09:24
// 树形结构导航模式
/*
树形结构导航模式也是非常重要的导航模式,它将导航视图控制器(UINavigationController)与表视图(UITableView)结合使用,主要用于构建有从属关系的导航。这种导航模式采用分层组织信息的方式,可以帮助我们构建ios效率型应用程序
*/
代码实现如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; self.window.backgroundColor = [UIColor grayColor]; [self.window makeKeyAndVisible]; KFViewController *viewCtl = [[KFViewController alloc] init]; self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewCtl]; self.window.rootViewController = self.navigationController; [self.navigationController release]; [viewCtl release]; return YES; // 树形结构导航 /* 树形结构导航模型中,有两个根视图控制器: 一个是应用程序的根视图控制器,它是UINavigationController的实例,通过self.window.rootViewController属性指定。 另一个是导航控制器根视图控制器,它一般是UIViewController的实例,通过UINavigationController的构造方法 initWithRootViewController:指定,用于提供和呈现导航控制器的一级视图,即我们看到的第一个界面。 */}
- (void)viewDidLoad{ [super viewDidLoad]; self.view.backgroundColor = [UIColor grayColor]; mutArr = [[NSMutableArray alloc] initWithCapacity:20]; for (int i = 0 ; i < 10 ; i ++) { NSString *str = [NSString stringWithFormat:@"苹果_%d",i]; [mutArr addObject:str]; } // 导航栏标题 self.title = @"苹果"; // 导航栏左边和右边按钮 UIBarButtonItem *navLeftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editClick:)]; UIBarButtonItem *navRightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneClick:)]; self.navigationItem.leftBarButtonItem = navLeftItem; self.navigationItem.rightBarButtonItem = navRightItem; [navLeftItem release]; [navRightItem release]; [self initTableView];}#pragma mark - 初始化UITableView- (void)initTableView{ m_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)]; // 568 m_tableView.backgroundColor = [UIColor whiteColor]; m_tableView.delegate = self; m_tableView.dataSource = self; [self.view addSubview:m_tableView]; [m_tableView release];}#pragma mark - UITableViewDelegate- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [mutArr count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *indentifier = @"myCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier] autorelease]; } NSString *str = [mutArr objectAtIndex:[indexPath row]]; cell.textLabel.text = str; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ int iRow = [indexPath row]; NSString *str = [mutArr objectAtIndex:iRow]; DetailViewController *detailViewCtl = [[DetailViewController alloc] init]; detailViewCtl.m_Title = str; [self.navigationController pushViewController:detailViewCtl animated:YES]; [detailViewCtl release];}- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ if (!tableView.editing) return UITableViewCellEditingStyleNone; else { return UITableViewCellEditingStyleDelete; }}- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ int iRow = [indexPath row]; [mutArr removeObjectAtIndex:iRow]; [tableView reloadData];}- (void)editClick:(id)sender{ m_tableView.editing = !m_tableView.editing;}- (void)doneClick:(id)sender{ m_tableView.editing = NO;}
//// DetailViewController.h//#import <UIKit/UIKit.h>@interface DetailViewController : UIViewController <UIWebViewDelegate>@property (copy, nonatomic) NSString *m_Title;@end
//// DetailViewController.m//#import "DetailViewController.h"@interface DetailViewController ()@end@implementation DetailViewController- (void)dealloc{ [self.m_Title release]; [super dealloc];}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; self.title = self.m_Title; [self intiUI];}- (void)intiUI{ UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)]; // 568 webView.backgroundColor = [UIColor clearColor]; webView.delegate = self; [self.view addSubview:webView]; [webView release]; NSString *str = @"http://www.baidu.com"; NSURL *url = [NSURL URLWithString:str]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [webView loadRequest:request];}- (void)webViewDidFinishLoad:(UIWebView *)webView{ NSLog(@"finish");}- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ NSLog(@"%@",[error description]);}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning];}@end
至此,树形结构导航介绍已经完毕,程序运行效果图如下:
ios 6效果图:
ios 7效果图:
0 0
- ios-使用树形结构导航模式
- ios-使用平铺导航模式
- ios-使用标签导航模式
- 使用SQL语句生成站点导航树形结构的实例
- 【JS 设计模式 】用组合模式来实现树形导航--代码结构思路分析(一)
- Objective-C之平铺导航、标签导航、树形结构导航
- IOS导航控制器结构
- 树形结构的使用
- 【JS 设计模式 】用组合模式来实现树形导航--JS代码结构思路分析(二)
- 组合模式:输出树形结构
- 组合模式 -- 树形结构处理
- iOS导航模式
- iOS导航模式综述
- 使用递归制作树形结构
- 单实用SQL脚本Part:生成站点导航树形结构
- Xcode_7 iOS_9 树形结构导航 Objective-C (17)
- iOS导航栏使用
- JAVA组合模式应用-树形结构
- [UnitTest03]-Moq
- java 集合框架JCF
- Shell - seq 的一般用途和它相若的命令
- 内存分配(堆、栈、BSS、代码段、数据段)
- win7怎样开启loopback接口(环回网卡)
- ios-使用树形结构导航模式
- 几个主流TCP/IP协议栈介绍
- MG网络技术运营
- 碰撞检测
- Java IO流
- LDAP分页查询
- [寒江孤叶丶的CrossApp之旅_08][入门系列]CASegmentedControl的简单介绍
- Oracle 12c及oracle sql developer登陆scott时都出现ORA-01017错误的解决方法
- 第一课(3)STM32_RCC