oc界面开发整理
oc界面开发整理
ViewController.h from test82
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource,UISearchBarDelegate, UISearchResultsUpdating> @end
ViewController.m
#import "ViewController.h" @interface ViewController ()
@property NSArray *tableData;
@property NSArray *searchData;
@property UISearchController *searchController;
@end @implementation ViewController
@synthesize tableData;
@synthesize searchData;
@synthesize searchController;
bool isSearch;
UITableView *tableView; - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
isSearch = NO;
tableData = [NSArray arrayWithObjects:@"A1",
@"A2",
@"A3",
@"A4",
@"A5",
@"A6",
@"A7",
@"A8",
@"A9",
@"A10",
@"oc1",
@"oc2",
@"oc3",
@"oc4",
@"oc5",
@"oc6",
@"oc7",
@"oc8",
@"oc9",
@"oc10",
@"oc11",
@"oc12",
@"oc13",
@"oc14",
@"oc15",
@"oc16",
@"oc17",
@"oc18",
@"oc19",
@"oc20",
@"oc21",
@"oc22",
@"oc23",
@"oc24",
@"oc25",
@"oc26",
@"oc27",
@"oc28",
@"oc29",
@"oc30",nil];
tableView = [[UITableView alloc] initWithFrame:self.view.bounds ];
// 手动管理tableView偏移
if(@available(iOS 11.0, *)){
NSLog(@"");
tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
self.edgesForExtendedLayout = UIRectEdgeNone;
if(@available(iOS 11.0, *)){
tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}else{
self.automaticallyAdjustsScrollViewInsets = NO;
}
self.definesPresentationContext = YES; tableView.delegate = self;
tableView.dataSource = self;
tableView.contentInset = UIEdgeInsetsMake(, , , );
tableView.scrollIndicatorInsets = tableView.contentInset;
// tableView.contentOffset = CGPointMake(0, -54);
[self.view addSubview:tableView]; // UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 44)];
searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
// 设置结果代理回调
searchController.searchResultsUpdater = self;
// 设置searchBar回调
searchController.searchBar.delegate = self;
searchController.dimsBackgroundDuringPresentation = NO;
searchController.hidesNavigationBarDuringPresentation = NO; // 作为UITableView的tableHeaderView 可能导致向上滚动的时候一起都滚动了
// tableView.tableHeaderView = searchController.searchBar;
// 添加到UINavigationBar 没有navigationItem,添加之后没有显示
// self.navigationItem.titleView = searchController.searchBar;
// 添加到其它视图 直接添加太靠上了;
// [self.view addSubview:searchController.searchBar];
// 通过一个UIView进行视图的叠加添加
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(, , self.view.bounds.size.width, )];
[view1 addSubview:searchController.searchBar];
[self.view addSubview:view1]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(isSearch){
return searchData.count;
}else{
return tableData.count;
}
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = @"cellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
if(isSearch){
cell.textLabel.text = [searchData objectAtIndex:indexPath.row];
}else{
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
}
return cell;
} - (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
if([searchController.searchBar.text length]>){
NSLog(@"%@",searchController.searchBar.text);
[self filter: searchController.searchBar.text];
}else{
NSLog(@"");
isSearch = NO;
[tableView reloadData];
}
} - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
NSLog(@"");
isSearch = NO;
[tableView reloadData];
} -(void)filter:(NSString *)substr{
isSearch = YES;
NSPredicate *pred = [NSPredicate predicateWithFormat:@"self contains[c] %@",substr];
searchData = [tableData filteredArrayUsingPredicate:pred];
[tableView reloadData];
} @end
AppDelegate
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.window.rootViewController = viewController;
StoryBoard获取不同id的Controller
ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"list"];
获取plist文件中数据
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"team" ofType:@"plist"]; // 获取属性列表文件中的全部数据
self.listTeams = [[NSArray alloc] initWithContentsOfFile:plistPath];
TableViewController的NavigationController操作
BookController.h from test68
#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface BookViewController : UITableViewController
// 定义两个NSMutableArray对象,分别保存图书名和图书详情
@property(nonatomic, strong) NSMutableArray *books;
@property(nonatomic, strong) NSMutableArray *details; @end NS_ASSUME_NONNULL_END
BookController.m
#import <QuartzCore/QuartzCore.h>
#import "BookViewController.h"
#import "EditViewController.h"
#import "HeaderViewController.h" @interface BookViewController () @end @implementation BookViewController
@synthesize books;
@synthesize details; - (void)viewDidLoad {
[super viewDidLoad];
// 创建、并初始化NSArray对象
books = [NSMutableArray arrayWithObjects:@"C",
@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",
@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",
@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",@"OC",
nil];
// 创建、并初始化NSArray对象
details = [NSMutableArray arrayWithObjects:@"C detail",
@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",
@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",
@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",@"OC detail",
nil];
// 设置当前视图关联的导航项的标题
self.navigationItem.title = @"图书列表"; UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
[button setTitle:@"click" forState:UIControlStateNormal];
[headerView addSubview:button];
headerView.backgroundColor = [UIColor redColor]; HeaderViewController *headerViewController = [[HeaderViewController alloc] init]; UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
view2.backgroundColor = [UIColor redColor];
[view2 addSubview:headerViewController.view]; // tableView表头添加技巧:使用一个UIView进行xib界面的包裹,然后把tableView的headerView设置为该UIView;声明UIView的时候指定尺寸;
self.tableView.tableHeaderView = view2;
// self.tableView.tableHeaderView = headerViewController.view;
// self.tableView.tableHeaderView = headerView; } - (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.tableView reloadData];
} // 该方法返回值决定各表格行的控件。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 为表格行定义一个静态字符串作为标识
static NSString *cellId = @"cellId";
// 从可重用表格行的队列中取出一个表格行
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
// 如果取出的表格行为nil
if(!cell){
// 创建一个UITableViewCell对象,使用UITableViewCellStyleSubtitle风格
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
// 从IndexPath参数中获取当前行号
NSUInteger rowNo = indexPath.row;
// 取出books中索引为rowNo的元素作为UITableViewCell的文本标题
cell.textLabel.text = [books objectAtIndex:rowNo];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; // 复合样式
// cell.accessoryType = UITableViewCellAccessoryCheckmark; // 对号图标样式
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 规则的前进箭头式样
// cell.accessoryType = UITableViewCellAccessoryNone; // 无附加视图
// cell.accessoryType = UITableViewCellAccessoryDetailButton; // 详情按钮样式
// 取出details中索引为rowNo的元素作为UITableViewCell的详细内容
cell.detailTextLabel.text = [details objectAtIndex:rowNo];
return cell;
} #pragma mark - Table view data source
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// return 3;
//}
// 该方法的返回值决定指定分区内包含多少个表格行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// 由于该表格只有一个分区,直接返回books中集合元素个数就代表表格的行数
return [books count];
} // UITableViewDelegate定义的方法,当表格行右边的附件按钮被单击时激发该方法
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
// 获取表格行号
NSInteger rowNo = indexPath.row;
EditViewController *editController = [[EditViewController alloc] init];
// 将被单击的表格行的数据传递给editController对象
editController.name = [books objectAtIndex:rowNo];
editController.detail = [details objectAtIndex:rowNo];
editController.rowNo = rowNo;
editController.bookViewController = self;
// 将editController压入到UINavigationController管理的控制栈中
[self.navigationController pushViewController:editController animated:YES]; } @end
表格附属按钮点击事件和表格行点击事件
// 表格行点击事件 from test74
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"detail" ];
detailViewController.editingIndexPath = indexPath;
appDelegate.window.rootViewController = detailViewController; } // UITableViewDelegate定义的方法,当表格行右边的附件按钮被单击时激发该方法 from test66
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
// 获取表格行号
NSInteger rowNo = indexPath.row;
EditViewController *editController = [[EditViewController alloc] init]; // 将被单击表格行的数据传递给editController控制器对象
editController.name = [books objectAtIndex:rowNo];
editController.detail = [details objectAtIndex:rowNo];
editController.rowNo = rowNo;
// 将editController压入UINavigationController管理的控制器中
[self.navigationController pushViewController:editController animated:YES]; }
动画切换rootViewController
- (void)restoreRootViewController:(UIViewController *)rootViewController
{ typedef void (^Animation)(void);
UIWindow* window = self.window; Animation animation = ^{
BOOL oldState = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
window.rootViewController = rootViewController;
[UIView setAnimationsEnabled:oldState];
}; [UIView transitionWithView:window
duration:0.5f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:animation
completion:nil];
}
oc界面开发整理的更多相关文章
- DevExpress .NET界面开发示例大全
说到做.net界面开发,很多人应该都会想到DevExpress. 它的 .net界面开发系列一共有7个版本:WinForms.ASP.NET.MVC.WPF.Silverlight.Windows 8 ...
- winform界面开发-HTML内容编辑控件
参照及推荐博客:伍华聪 http://www.cnblogs.com/wuhuacong/archive/2009/07/07/1518346.html http://www.cnblogs.com/ ...
- 全球首个全流程跨平台界面开发套件,PowerUI分析
一. 首个全流程跨平台界面开发套件,PowerUI正式发布 UIPower在DirectUI的基础上,自主研发全球首个全流程跨平台界面开发套件PowerUI(PUI)正式发布,PowerU ...
- HTML5界面开发工具jQuery EasyUI更新至v1.3.5
本文转自:evget.com HTML5界面开发工具 jQuery EasyUI 最新发布v1.3.5,新版修复了多个bug,并改进了menu,tabs和slider等多个控件.jQuery Easy ...
- iOS界面开发
[转载] iOS界面开发 发布于:2014-07-29 11:49阅读数:13399 iOS 8 和 OS X 10.10 中一个被强调了多次的主题就是大一统,Apple 希望通过 Hand-off ...
- WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用
WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用 转自:http://www.cnblogs.com/wuhuacong/arch ...
- [GUI]界面开发类库-Ribbon风格 [转]
[GUI]界面开发类库 如果我们不十分清楚需要什么样的界面风格及如何实现,请按以下两个步骤操作: (1) 搞清楚这种风格叫什么名字 (2) 查现有的比较著名的GUI库是否已有相 ...
- JAVA与图形界面开发(Applet应用程序、AWT库、Swing)
Applet 1)简单说,Applet就是嵌入到网页中的小程序,Java代码. 2)编写Applet程序,要继承JApplet类,并根据自己需要覆写相关方法(init.start.stop.destr ...
- [GUI]界面开发类库
如果我们不十分清楚需要什么样的界面风格及如何实现,请按以下两个步骤操作: (1) 搞清楚这种风格叫什么名字 (2) 查现有的比较著名的GUI库是否已有相应的实现方案. (3) ...
随机推荐
- 32、flex布局
html: <div class="parent"> <div class="son">1</div> <div cl ...
- 浅谈HTML5的新特性
2014年10月29日,W3C宣布,经过接近8年的艰苦努力,HTML5标准规范终于制定完成. HTML5将会取代1999年制定的HTML 4.01.XHTML 1.0标准,使网络标准达到符合当代的网络 ...
- react 爬坑记录
1.父子组件优化其一发生render条件:数据改变(state或者props改变),有时子组件会过多render.这时可在子组件里面的生命周期钩子里执行 shouldComponentUpdate(n ...
- ThinkPHP5中如何实现模板完全静态化
模板完全静态化,也就是通过模板完全生成纯静态的网页,相比动态页面和伪静态页面更安全更利于SEO访问更快.相比前二者各有利弊吧,现在稍微对这三种形式的优缺点对比一下,以及在ThinkPHP5项目中实现完 ...
- Odoo中的Widget
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826144.html 一:Widget是什么 Odoo中定义了字段的显示形式,不同字段类型的字段都有其不同 ...
- 一键配置树莓派wifi、静态ip等
系统老是被我玩坏,重新烧录系统后配置又太麻烦,用shell脚本一键配置一下wifi和静态IP #!/bin/bash touch /media/fanghaoyu/boot/ssh echo &quo ...
- HTML-Parser
背景:需求需要把 html 字符串转成 DOM 对象树或者 js 对象树,然后进行一些处理/操作.htmlparser 这个库还行,但是对 attribute 上一些特殊属性值转换不行,同时看了看`开 ...
- SpringCloud2.0 Eureka Server 服务中心 基础教程(二)
1.创建[服务中心],即 Eureka Server 1.1.新建 Spring Boot 工程,工程名称: springcloud-eureka-server 1.2.工程 pom.xml 文件添加 ...
- vue.js生成纵向拓扑图
1.前端代码 <link href="https://magicbox.bk.tencent.com/static_api/v3/assets/bootstrap-3.3.4/css/ ...
- 项目Beta冲刺(2/7)(追光的人)(2019.5.24)
所属课程 软件工程1916 作业要求 Beta冲刺博客汇总 团队名称 追光的人 作业目标 描述Beta冲刺每日的scrum和PM报告两部分 队员学号 队员博客 221600219 小墨 https:/ ...