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界面开发整理的更多相关文章

  1. DevExpress .NET界面开发示例大全

    说到做.net界面开发,很多人应该都会想到DevExpress. 它的 .net界面开发系列一共有7个版本:WinForms.ASP.NET.MVC.WPF.Silverlight.Windows 8 ...

  2. winform界面开发-HTML内容编辑控件

    参照及推荐博客:伍华聪 http://www.cnblogs.com/wuhuacong/archive/2009/07/07/1518346.html http://www.cnblogs.com/ ...

  3. 全球首个全流程跨平台界面开发套件,PowerUI分析

    一.       首个全流程跨平台界面开发套件,PowerUI正式发布 UIPower在DirectUI的基础上,自主研发全球首个全流程跨平台界面开发套件PowerUI(PUI)正式发布,PowerU ...

  4. HTML5界面开发工具jQuery EasyUI更新至v1.3.5

    本文转自:evget.com HTML5界面开发工具 jQuery EasyUI 最新发布v1.3.5,新版修复了多个bug,并改进了menu,tabs和slider等多个控件.jQuery Easy ...

  5. iOS界面开发

    [转载] iOS界面开发 发布于:2014-07-29 11:49阅读数:13399 iOS 8 和 OS X 10.10 中一个被强调了多次的主题就是大一统,Apple 希望通过 Hand-off ...

  6. WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用

    WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用 转自:http://www.cnblogs.com/wuhuacong/arch ...

  7. [GUI]界面开发类库-Ribbon风格 [转]

    [GUI]界面开发类库 如果我们不十分清楚需要什么样的界面风格及如何实现,请按以下两个步骤操作: (1)       搞清楚这种风格叫什么名字 (2)       查现有的比较著名的GUI库是否已有相 ...

  8. JAVA与图形界面开发(Applet应用程序、AWT库、Swing)

    Applet 1)简单说,Applet就是嵌入到网页中的小程序,Java代码. 2)编写Applet程序,要继承JApplet类,并根据自己需要覆写相关方法(init.start.stop.destr ...

  9. [GUI]界面开发类库

    如果我们不十分清楚需要什么样的界面风格及如何实现,请按以下两个步骤操作: (1)       搞清楚这种风格叫什么名字 (2)       查现有的比较著名的GUI库是否已有相应的实现方案. (3)  ...

随机推荐

  1. Ubuntu下 安卓 adb 命令报:“insufficient permissions for device: user in plugdev group; ”问题的解决办法

    https://blog.csdn.net/freezingxu/article/details/80893025 在接入设备进行联机调试的时候,遇到了这样的问题: insufficient perm ...

  2. RabbitMQ基本概念(四)-服务详细配置与日常监控管理

    RabbitMQ服务管理 启动服务:rabbitmq-server -detached[ /usr/local/rabbitmq/sbin/rabbitmq-server -detached ] 查看 ...

  3. postgresql基于备份点PITR恢复

    实验目的: 01.基于备份点直接恢复数据库 02.基于备份点后续增量wal日志恢复到特定的时间点 实验环境: centos7 postgresql9.5 01.安装postgresql9.5 post ...

  4. manjaro手动安装Redis

    以前都是用的Windows系统,最近有被win10搞得有点烦,就入了manjaro的坑,windows下部分软件在manjaro安装记录,留个记录. 我的系统信息 下面开始正式干活. 一.准备步骤 下 ...

  5. [FreeRTOS].FreeRTOS CortexM3 M4中断优先级设置总结

    转自:https://blog.csdn.net/xukai871105/article/details/53516857 前言本文将说明在FreeRTOS嵌入式操作系统中,如何设置STM32 Cor ...

  6. ERROR 1129 (HY000): Host '192.168.7.210' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'

    一.问题现象 mysql远程连接报错 ERROR (HY000): Host '192.168.7.210' is blocked because of many connection errors; ...

  7. GitLab企业级代码管理仓库

    原文:https://www.cnblogs.com/wsnbba/p/10171052.html   使用GitHub或者码云等公共代码仓库 使用GitLab私有仓库 GitLab是什么? 是一个用 ...

  8. 模型融合---为什么说bagging是减少variance,而boosting是减少bias?

    1.bagging减少variance Bagging对样本重采样,对每一重采样得到的子样本集训练一个模型,最后取平均.由于子样本集的相似性以及使用的是同种模型,因此各模型有近似相等的bias和var ...

  9. 项目Alpha冲刺(团队)-博客汇总

    格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队) 团队名称:为了交项目干杯 作业目标:集中记录所有敏捷冲刺日志的集合 团队信息 队员学号 队员姓名 个人博客地 ...

  10. Spring Boot 之:Actuator 监控

    在 Spring Boot 2.x 中为了安全,Actuator 只开放了两个端点 /actuator/health 和 /actuator/info.可以在配置文件中设置打开. Actuator 默 ...