oc界面開發整理

oc界面開發整理app

ViewController.h from test82ide

#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

AppDelegateatom

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.window.rootViewController = viewController;

StoryBoard獲取不一樣id的Controllerspa

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操做rest

BookController.h from test68code

#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.morm

#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];
}
相關文章
相關標籤/搜索