iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码
废话不多说了,直接给大家贴代码了。
具体代码如下所示:
#import"ViewController.h" #import"TuanGouModel.h" #import"TuanGouTableViewCell.h" #definekDeviceWidth[UIScreenmainScreen].bounds.size.width #definekDeviceHeight[UIScreenmainScreen].bounds.size.height @interfaceViewController()<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating> { UISearchController*_sscller; } @property(nonatomic,strong)NSMutableArray*secArrM; @property(nonatomic,strong)NSMutableArray*tuanGouArrM; @property(nonatomic,strong)UITableView*myTable; @end @implementationViewController -(void)viewDidLoad{ [superviewDidLoad]; [selfcreateNa]; self.myTable.backgroundColor=[UIColorlightGrayColor]; [selfcreatesecB]; [selfsetupRefresh]; self.title=@"美食家"; } #pragmamark-导航 -(void)createNa{ UIBarButtonItem*rightItem=[[UIBarButtonItemalloc]initWithTitle:@"Edit"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(tableEdit:)]; self.navigationItem.rightBarButtonItem=rightItem; self.title=@"美食家"; } //点击导航右侧编辑按钮时,让表格可编辑 -(void)tableEdit:(UIBarButtonItem*)btnItem{ //if(self.myTable.editing==NO){//没有处于编辑状态,导航按钮文字为“Edit” ////点击“编辑”文字,让表格处于编辑状态,并把按钮的文字修改为“Done" //self.myTable.editing=YES; // //}else{ ////编辑状态下,点击”Done"按钮,取消表格的编辑状态,修改导航按钮文字为"Edit" //self.myTable.editing=NO; //btnItem.title=@"Edit"; //self.navigationItem.rightBarButtonItems=@[btnItem]; //} } -(void)createsecB{ _sscller=[[UISearchControlleralloc]initWithSearchResultsController:nil]; _sscller.searchResultsUpdater=self; self.myTable.tableHeaderView=_sscller.searchBar; } -(NSMutableArray*)secArrM{ if(_secArrM==nil){ return_secArrM=[NSMutableArrayarray]; }else{ return_secArrM; } } -(void)didReceiveMemoryWarning{ [superdidReceiveMemoryWarning]; } #pragmamark-表格懒加载 -(UITableView*)myTable{ if(_myTable==nil){ _myTable=[[UITableViewalloc]initWithFrame:CGRectMake(,,kDeviceWidth,kDeviceHeight)style:UITableViewStylePlain]; [self.viewaddSubview:_myTable]; _myTable.delegate=self; _myTable.dataSource=self; _myTable.separatorStyle=UITableViewCellSeparatorStyleSingleLineEtched; } return_myTable; } #pragmamark-团购数据懒加载 -(NSMutableArray*)tuanGouArrM{ if(_tuanGouArrM==nil){ _tuanGouArrM=[NSMutableArrayarray]; NSString*plistPath=[[NSBundlemainBundle]pathForResource:@"tgs.plist"ofType:nil]; NSArray*tuanArr=[NSArrayarrayWithContentsOfFile:plistPath]; for(NSDictionary*dictintuanArr){ TuanGouModel*model=[[TuanGouModelalloc]initWithDict:dict]; [_tuanGouArrMaddObject:model]; } } return_tuanGouArrM; } #pragmamark-数据源协议 -(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{ if(_sscller.active){//搜索结果表格 returnself.secArrM.count; } else{ returnself.tuanGouArrM.count; } } -(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{ //注册 [tableViewregisterClass:[TuanGouTableViewCellclass]forCellReuseIdentifier:@"tuanCell"]; //重置 TuanGouTableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:@"tuanCell"forIndexPath:indexPath]; cell.backgroundColor=[UIColoryellowColor]; //选中风格 cell.selectionStyle=UITableViewCellSelectionStyleNone; if(!_sscller.active){ cell.tuanGouModel=self.tuanGouArrM[indexPath.row]; }else{//搜索结果 cell.tuanGouModel=self.secArrM[indexPath.row]; } returncell; } #pragmamark-TableV协议 -(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath{ return; } -(void)updateSearchResultsForSearchController:(UISearchController*)searchController{ [self.secArrMremoveAllObjects]; for(intj=;j<_tuanGouArrM.count;j++){ TuanGouModel*model=[[TuanGouModelalloc]init]; model=_tuanGouArrM[j]; if([model.titleisEqualToString:_sscller.searchBar.text]){ [self.secArrMaddObject:model]; } } [self.myTablereloadData]; } //允许Menu菜单 -(BOOL)tableView:(UITableView*)tableViewshouldShowMenuForRowAtIndexPath:(NSIndexPath*)indexPath { returnYES; } //每个cell都可以点击出现Menu菜单 -(BOOL)tableView:(UITableView*)tableViewcanPerformAction:(SEL)actionforRowAtIndexPath:(NSIndexPath*)indexPathwithSender:(id)sender { returnYES; } -(void)tableView:(UITableView*)tableViewperformAction:(SEL)actionforRowAtIndexPath:(NSIndexPath*)indexPathwithSender:(id)sender{ NSLog(@"长按"); if(action==@selector(copy:)){ NSLog(@"copy"); } if(action==@selector(cut:)){ NSLog(@"cut"); } if(action==@selector(paste:)){ NSLog(@"paste"); } if(action==@selector(selectAll:)){ NSLog(@"selectAll"); } } //上拉加载 -(void)tableView:(UITableView*)tableViewwillDisplayCell:(UITableViewCell*)cellforRowAtIndexPath:(NSIndexPath*)indexPath{ if(indexPath.row==self.tuanGouArrM.count-){ NSLog(@"最后一行"); TuanGouModel*model=[[TuanGouModelalloc]init]; model=_tuanGouArrM[arcrandom()%]; [_tuanGouArrMaddObject:model]; [self.myTablereloadData]; } } //下拉刷新 -(void)setupRefresh { //.添加刷新控件 UIRefreshControl*control=[[UIRefreshControlalloc]init]; [controladdTarget:selfaction:@selector(refreshStateChange:)forControlEvents:UIControlEventValueChanged]; [self.myTableaddSubview:control]; //.马上进入刷新状态,并不会触发UIControlEventValueChanged事件 [controlbeginRefreshing]; //.加载数据 [selfrefreshStateChange:control]; } /** *UIRefreshControl进入刷新状态:加载最新的数据 */ -(void)refreshStateChange:(UIRefreshControl*)control { TuanGouModel*model=[[TuanGouModelalloc]init]; model=_tuanGouArrM[arcrandom()%]; [_tuanGouArrMinsertObject:modelatIndex:]; [self.myTablereloadData]; NSLog(@"第一行"); [controlendRefreshing]; } //指示是否允许高亮显示选中的行 -(BOOL)tableView:(UITableView*)tableViewshouldHighlightRowAtIndexPath:(NSIndexPath*)indexPath{ returnYES; } //选中某行时执行 -(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{ NSLog(@"selected:%ld,row:%ld",indexPath.section,indexPath.row); } //取消选中时执行,这个方法常在表格允许多选时调用执行 -(void)tableView:(UITableView*)tableViewdidDeselectRowAtIndexPath:(NSIndexPath*)indexPath{ NSLog(@"Deselected:%ld,row:%ld",indexPath.section,indexPath.row); }
以上代码是hi小编给大家介绍的iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码,希望对大家有所帮助!