zoukankan      html  css  js  c++  java
  • UITableView与UISearchController搜索及上拉加载,下拉刷新

      1 #import "ViewController.h"
      2 #import "TuanGouModel.h"
      3 #import "TuanGouTableViewCell.h"
      4 #define kDeviceWidth [UIScreen mainScreen].bounds.size.width
      5 #define kDeviceHeight [UIScreen mainScreen].bounds.size.height
      6 @interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating>
      7 {
      8     UISearchController * _sscller;
      9 }
     10 @property(nonatomic,strong)NSMutableArray* secArrM;
     11 
     12 @property(nonatomic,strong) NSMutableArray* tuanGouArrM;
     13 
     14 @property(nonatomic,strong)UITableView* myTable;
     15 
     16 @end
     17 
     18 @implementation ViewController
     19 
     20 - (void)viewDidLoad {
     21     
     22     [super viewDidLoad];
     23     [self createNa];
     24     self.myTable.backgroundColor = [UIColor lightGrayColor];
     25     [self createsecB];
     26     [self setupRefresh];
     27     
     28     self.title = @"美食家";
     29 }
     30 
     31 #pragma mark - 导航
     32 -(void)createNa{
     33     
     34     UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(tableEdit:)];
     35     self.navigationItem.rightBarButtonItem = rightItem;
     36     self.title = @"美食家";
     37 
     38 }
     39 
     40 // 点击导航右侧编辑按钮时,让表格可编辑
     41 -(void)tableEdit:(UIBarButtonItem *) btnItem{
     42     
     43 //    if (self.myTable.editing == NO ) {      // 没有处于编辑状态,导航按钮文字为“Edit”
     44 //        // 点击“编辑”文字,让表格处于编辑状态,并把按钮的文字修改为“Done"
     45 //        self.myTable.editing = YES;
     46 // 
     47 //    }else{
     48 //        // 编辑状态下,点击”Done"按钮,取消表格的编辑状态,修改导航按钮文字为"Edit"
     49 //        self.myTable.editing = NO;
     50 //        btnItem.title = @"Edit" ;
     51 //        self.navigationItem.rightBarButtonItems = @[btnItem];
     52 //    }
     53     
     54 }
     55 
     56 -(void)createsecB{
     57     _sscller = [[UISearchController alloc]initWithSearchResultsController:nil];
     58     _sscller.searchResultsUpdater = self;
     59     self.myTable.tableHeaderView = _sscller.searchBar;
     60     
     61     
     62 }
     63 -(NSMutableArray *)secArrM{
     64     
     65     
     66     if (_secArrM == nil) {
     67        return _secArrM = [NSMutableArray array];
     68         
     69     }else{
     70          return _secArrM;
     71     }
     72     
     73     
     74    
     75 }
     76 
     77 - (void)didReceiveMemoryWarning {
     78     
     79     [super didReceiveMemoryWarning];
     80     
     81 }
     82 #pragma mark - 表格懒加载
     83 -(UITableView *)myTable{
     84     if (_myTable == nil) {
     85         _myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight) style:UITableViewStylePlain];
     86         [self.view addSubview:_myTable];
     87         
     88         _myTable.delegate = self;
     89         _myTable.dataSource = self;
     90         _myTable .separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
     91         
     92     }
     93     return _myTable;
     94 
     95 }
     96 
     97 #pragma mark - 团购数据懒加载
     98 -(NSMutableArray *)tuanGouArrM{
     99 
    100     if (_tuanGouArrM == nil) {
    101         _tuanGouArrM = [NSMutableArray array];
    102         NSString* plistPath = [[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil];
    103         NSArray* tuanArr = [NSArray arrayWithContentsOfFile:plistPath];
    104         
    105         for (NSDictionary* dict in tuanArr) {
    106             TuanGouModel* model =[[TuanGouModel alloc]initWithDict:dict];
    107             [_tuanGouArrM addObject:model];
    108         }
    109     }
    110     return _tuanGouArrM;
    111 }
    112 
    113 #pragma mark - 数据源协议
    114 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    115     if ( _sscller.active ) {        //搜索结果表格
    116         return  self.secArrM.count;
    117     }
    118     
    119     else{
    120         return self.tuanGouArrM.count;
    121 
    122    }
    123 }
    124 
    125 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    126  
    127     //注册
    128     [tableView registerClass:[TuanGouTableViewCell class] forCellReuseIdentifier:@"tuanCell"];
    129     //重置
    130     TuanGouTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"tuanCell"forIndexPath:indexPath];
    131     cell.backgroundColor = [UIColor yellowColor];
    132     // 选中风格
    133     cell.selectionStyle = UITableViewCellSelectionStyleNone;
    134     if( !_sscller.active ){
    135        cell.tuanGouModel = self.tuanGouArrM[indexPath.row];
    136     }else{          //搜索结果
    137        cell.tuanGouModel = self.secArrM[indexPath.row];
    138     }
    139     
    140 
    141     return cell;
    142 }
    143 #pragma mark - TableV协议
    144 
    145 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    146 
    147     return 80;
    148 }
    149 
    150 -(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    151     
    152     [self.secArrM removeAllObjects];
    153         for (int j = 0; j < _tuanGouArrM.count; j++) {
    154             TuanGouModel* model =[[TuanGouModel alloc]init];
    155                  model = _tuanGouArrM[j];
    156             if ([model.title isEqualToString: _sscller.searchBar.text]) {
    157                 [self.secArrM addObject: model];
    158             }
    159     }
    160     
    161     [self.myTable reloadData];
    162 }
    163 
    164 
    165 //允许Menu菜单
    166 -(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
    167 {
    168     return   YES;
    169 }
    170 
    171 //每个cell都可以点击出现Menu菜单
    172 -(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
    173 {
    174     return   YES;
    175 
    176 }
    177 -(void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    178 
    179     NSLog(@"长按");
    180     
    181     if (action ==@selector(copy:)) {
    182         
    183        NSLog(@"copy");
    184         
    185     }
    186     if (action ==@selector(cut:)) {
    187         NSLog(@"cut");
    188         
    189     }
    190     
    191     if (action ==@selector(paste:)) {
    192         
    193        
    194         NSLog(@"paste");
    195     }
    196 
    197     if (action ==@selector(selectAll:)) {
    198         
    199         
    200         NSLog(@"selectAll");
    201     }
    202 
    203 
    204 }
    205 //上拉加载
    206 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    207     if (indexPath.row == self.tuanGouArrM.count - 1) {
    208         NSLog(@"最后一行");
    209         
    210         TuanGouModel* model =[[TuanGouModel alloc]init];
    211         model = _tuanGouArrM[arc4random()%12];
    212         [_tuanGouArrM addObject:model];
    213         [self.myTable reloadData];
    214 
    215     }
    216 }
    217 
    218 //下拉刷新
    219 -(void)setupRefresh
    220 {
    221     //1.添加刷新控件
    222     UIRefreshControl *control=[[UIRefreshControl alloc]init];
    223     [control addTarget:self action:@selector(refreshStateChange:) forControlEvents:UIControlEventValueChanged];
    224     [self.myTable addSubview:control];
    225     
    226     //2.马上进入刷新状态,并不会触发UIControlEventValueChanged事件
    227     [control beginRefreshing];
    228     
    229     // 3.加载数据
    230     [self refreshStateChange:control];
    231 }
    232 
    233 
    234 /**
    235  *  UIRefreshControl进入刷新状态:加载最新的数据
    236  */
    237 -(void)refreshStateChange:(UIRefreshControl *)control
    238 {
    239     TuanGouModel* model =[[TuanGouModel alloc]init];
    240     model = _tuanGouArrM[arc4random()%12];
    241     [_tuanGouArrM insertObject:model atIndex:0];
    242     [self.myTable reloadData];
    243 
    244      NSLog(@"第一行");
    245      [control endRefreshing];
    246     
    247 }
    248 
    249 //指示是否允许高亮显示选中的行
    250 - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
    251     
    252     return YES;
    253 }
    254 
    255 //选中某行时执行
    256 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    257     NSLog(@"selected: %ld, row:%ld", indexPath.section, indexPath.row);
    258 }
    259 //取消选中时执行,这个方法常在表格允许多选时调用执行
    260 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    261     NSLog(@"Deselected: %ld, row:%ld", indexPath.section, indexPath.row);
    262 }

    源文件这里有http://pan.baidu.com/s/1pLlDm6f

    UITableView与UISearchController搜索及上拉加载,下拉刷新

  • 相关阅读:
    SynchronousQueue、LinkedBlockingQueue、ArrayBlockingQueue性能测试
    JDK源码分析—— ArrayBlockingQueue 和 LinkedBlockingQueue
    ArrayBlockingQueue和LinkedBlockingQueue的区别
    Net处理html页面元素工具类(HtmlAgilityPack.dll)的使用
    WebView混合开发
    对付"反盗链"
    通过代码来操作SQLite的示例
    System.Data.SQLite未能加载文件或程序集
    Using SQLXML Bulk Load in the .NET Environment
    Asynchronous Programming Using Delegates使用委托进行异步编程
  • 原文地址:https://www.cnblogs.com/ljcgood66/p/5411041.html
Copyright © 2011-2022 走看看