UIKit 框架之UISearchBar、UITableViewController

//
//  tableViewController.m
//  tableVC
//
//  Created by City--Online on 15/6/1.
//  Copyright (c) 2015年 CYW. All rights reserved.
//

#import "tableViewController.h"

@interface tableViewController ()<UISearchBarDelegate,UIScrollViewDelegate>
@property(nonatomic,strong) UISearchBar *searchBar;
@property(nonatomic,strong) NSArray *allData;
@property(nonatomic,strong) NSMutableArray *searchData;
@property(nonatomic,assign) NSInteger pageIndex;
@end

@implementation tableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _pageIndex=1;
    //初始化数据
    _allData=@[@"abcd11",@"abcd12",@"abcd13",@"abcd14"];
    _searchData=[_allData copy];
    //初始化UISearchBar
    _searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
    //样式
    _searchBar.barStyle=UISearchBarStyleDefault;
    //代理
    _searchBar.delegate=self;
    //文本内容
    _searchBar.text=@"abcd";
    //提示文字
    _searchBar.prompt=@"1234";
//    //文本为空时显示内容
//    _searchBar.placeholder=@"qazwsx";
//    //bookmark按钮是否显示
//    _searchBar.showsBookmarkButton=YES;
//    //取消按钮是否显示
//    _searchBar.showsCancelButton=YES;
//    //搜索结果按钮显示
//    _searchBar.showsSearchResultsButton=YES;
//
    //bar的颜色(具有渐变效果)
    _searchBar.tintColor=[UIColor redColor];
    _searchBar.barTintColor=[UIColor whiteColor];
    _searchBar.searchBarStyle=UISearchBarStyleMinimal;
    //是否透明
    _searchBar.translucent=NO;
//   Scope的内容
    _searchBar.scopeButtonTitles=@[@"English",@"China"];
    //默认选择的Scope索引
    _searchBar.selectedScopeButtonIndex=1;
    // 是否显示Scope
    _searchBar.showsScopeBar=YES;
    UIView *v=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    v.backgroundColor=[UIColor yellowColor];
    //设置辅助输入视图
    _searchBar.inputAccessoryView=nil;
    // 键盘样式
    _searchBar.keyboardType=UIKeyboardTypeDefault;
    // 返回按钮样式
    _searchBar.returnKeyType=UIReturnKeyDone;
    self.tableView.tableHeaderView=_searchBar;
    self.tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];

    //还有一些设置背景图片的一些属性方法 详见api
//    self.navigationItem.titleView=searchbar;

    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.modalPresentationCapturesStatusBarAppearance = NO;

}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.refreshControl=[[UIRefreshControl alloc]init];
    self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"啦啦啦啦儿童节"];
    [self.refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    [_searchBar resignFirstResponder];
    NSLog(@"%@", self.tableView.tableHeaderView.subviews);

}

-(void)refresh
{
    _pageIndex++;
    NSLog(@"1234");
    sleep(4);

    [self.refreshControl endRefreshing];
    [self.tableView reloadData];

}
//代理UISearchBarDelegate
//开始编辑
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    return YES;
}
// 将要结束编辑
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    NSLog(@"searchBarTextDidBeginEditing");
}
//是否可以结束编辑
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    return YES;
}
// 文本内容编辑结束
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
//    [searchBar resignFirstResponder];
    NSLog(@"searchBarTextDidEndEditing");
}
//文本内容改变
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"textDidChange");
    NSLog(@"%@",searchText);
    if (searchText.length==0) {
        [searchBar resignFirstResponder];
        _searchData=[_allData copy];
        [self.tableView reloadData];
    }

}
//文本改变之前是否可以改变
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    return YES;
}
//查询按钮点击
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF CONTAINS %@",searchBar.text];
    _searchData=[[_allData filteredArrayUsingPredicate:predicate] copy];
    [self.tableView reloadData];
}
//书签按钮点击
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"searchBarBookmarkButtonClicked");
}
//取消按钮点击
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];

}
// 搜索结果按钮点击
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"searchBarResultsListButtonClicked");

}
//Scope选择索引改变
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
    NSLog(@"%ld",selectedScope);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    // UIView默认的layoutMargins的值为 {8, 8, 8, 8}.在我们改变View的layoutMargins这个属性时,会触发- (void)layoutMarginsDidChange这个方法。我们在自己的View里面可以重写这个方法来捕获layoutMargins的变化。在大多数情况下,我们可以在这个方法里触发drawing和layout的Update。preservesSuperviewLayoutMargins这个属性默认是NO。如果把它设为YES,layoutMargins会根据屏幕中相关View的布局而改变。
#ifdef __IPHONE_8_0
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }

    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
#endif
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return _pageIndex;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _searchData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier=@"identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }
     cell.textLabel.text=[NSString stringWithFormat:@"%@",[_searchData objectAtIndex:indexPath.row] ];

    return cell;
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.

    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}

// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *v=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    v.backgroundColor=[UIColor yellowColor];
    return v;

}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 44;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

时间: 2024-09-03 12:03:43

UIKit 框架之UISearchBar、UITableViewController的相关文章

iOS UIKit 框架 346 篇文档分类整理 - 预告

iOS UIKit 框架 346 篇文档分类整理 - 预告 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 当前正在进行的是 "iOS Foundation 框架 224 篇相关文档分

UIKit 框架之UIView二

下面这些都是UIView一些基本的东西,具体的可以参考UIKit 框架之UIView一博客 一.自定义一个View // // MyView.m // UIView // // Created by cyw on 15-5-17. // Copyright (c) 2015年 cyw. All rights reserved. // #import "MyView.h" @implementation MyView - (id)initWithFrame:(CGRect)frame {

UIKit 框架之Bar、Controller

UIKit框架中有各种Bar,UITabBar.UINavigationBar.UIToolbar.Bar对应的就有一些Item,tabBarItem.navigationItem.toolbarItems,再加上UIViewController.UINavigationController.UITabBarController很容易搞糊涂.我看了好久,没看明白.动手敲了下才有一点感觉. 一.联系 一个UINavigationController对应着一个UINavigationBar.UITo

UIKit框架类层次图

学习UIKit应该首选了解UIKit类的层次图,从根类一层一层的拨.

UIKit 框架之UICollectionViewController

1.自定义单元格 #import <UIKit/UIKit.h> @interface myCollectionViewCell : UICollectionViewCell @property(nonatomic,strong) UIImageView *myImageView; @property(nonatomic,strong) UILabel *nameLabel; @end #import "myCollectionViewCell.h" @implementa

UIKit 框架之UIView一

- (id)initWithFrame:(CGRect)aRect //通过一个矩形对象初始化 Configuring a View's Visual Appearance //配置视觉展示 @property(nonatomic, copy) UIColor *backgroundColor //设置背景色 @property(nonatomic, getter=isHidden) BOOL hidden //隐藏view,默认为NO @property(nonatomic) CGFloat

UIKit 框架之UICollectionView

1.自定义UICollectionViewCell 在myCollectionViewCell.h中声明两个属性 // // myCollectionViewCell.h // UICollectionView // // Created by City--Online on 15/5/25. // Copyright (c) 2015年 XQB. All rights reserved. // #import <UIKit/UIKit.h> @interface myCollectionVi

UIKit框架之NSObject

首先学习NSObject  // // ViewController.m // localization // // Created by City--Online on 15/5/15. // Copyright (c) 2015年 XQB. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (voi

UIKit 框架之UIScrollView

理解UIScrollView时可以假设这样的一个场景:在一个漆黑的屋子里的墙上有一副大的画,墙是可以上下左右滚动,一个矩形的手电筒固定着,开始时画的左上角坐标与矩形光的左上角坐标对应.人可以用手电筒的光来看这幅画. // // ViewController.m // UIScrollView // // Created by City--Online on 15/5/20. // Copyright (c) 2015年 XQB. All rights reserved. // #import "