oc界面开发整理

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(@"333333333333333333333");
        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(94, 0, 149, 0);
    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(0, 40, self.view.bounds.size.width, 44)];
    [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]>0){
        NSLog(@"%@",searchController.searchBar.text);
        [self filter: searchController.searchBar.text];
    }else{
        NSLog(@"222222222222222222222");
        isSearch = NO;
        [tableView reloadData];
    }
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"111111111111111111111111");
    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(0, 0, 100, 20)];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 10)];
    [button setTitle:@"click" forState:UIControlStateNormal];
    [headerView addSubview:button];
    headerView.backgroundColor = [UIColor redColor];
    
    HeaderViewController *headerViewController = [[HeaderViewController alloc] init];
   
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    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 编译错误、符号与链接、加载


下一篇:2021 iOS 进阶学习视频推荐