iOS開發之UITableView與UISearchController實現搜尋及上拉載入,下拉重新整理執行個體代碼_IOS

來源:互聯網
上載者:User

廢話不多說了,直接給大家貼代碼了。

具體代碼如下所示:

#import "ViewController.h"#import "TuanGouModel.h"#import "TuanGouTableViewCell.h"#define kDeviceWidth [UIScreen mainScreen].bounds.size.width#define kDeviceHeight [UIScreen mainScreen].bounds.size.height@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating>{UISearchController * _sscller;}@property(nonatomic,strong)NSMutableArray* secArrM;@property(nonatomic,strong) NSMutableArray* tuanGouArrM;@property(nonatomic,strong)UITableView* myTable;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];[self createNa];self.myTable.backgroundColor = [UIColor lightGrayColor];[self createsecB];[self setupRefresh];self.title = @"美食家";}#pragma mark - 導航-(void)createNa{UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@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 = [[UISearchController alloc]initWithSearchResultsController:nil];_sscller.searchResultsUpdater = self;self.myTable.tableHeaderView = _sscller.searchBar;}-(NSMutableArray *)secArrM{if (_secArrM == nil) {return _secArrM = [NSMutableArray array];}else{return _secArrM;}}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}#pragma mark - 表格懶載入-(UITableView *)myTable{if (_myTable == nil) {_myTable = [[UITableView alloc]initWithFrame:CGRectMake(, , kDeviceWidth, kDeviceHeight) style:UITableViewStylePlain];[self.view addSubview:_myTable];_myTable.delegate = self;_myTable.dataSource = self;_myTable .separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;}return _myTable;}#pragma mark - 團購資料懶載入-(NSMutableArray *)tuanGouArrM{if (_tuanGouArrM == nil) {_tuanGouArrM = [NSMutableArray array];NSString* plistPath = [[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil];NSArray* tuanArr = [NSArray arrayWithContentsOfFile:plistPath];for (NSDictionary* dict in tuanArr) {TuanGouModel* model =[[TuanGouModel alloc]initWithDict:dict];[_tuanGouArrM addObject:model];}}return _tuanGouArrM;}#pragma mark - 資料來源協議-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{if ( _sscller.active ) { //搜尋結果表格return self.secArrM.count;}else{return self.tuanGouArrM.count;}}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{//註冊[tableView registerClass:[TuanGouTableViewCell class] forCellReuseIdentifier:@"tuanCell"];//重設TuanGouTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"tuanCell"forIndexPath:indexPath];cell.backgroundColor = [UIColor yellowColor];// 選中風格cell.selectionStyle = UITableViewCellSelectionStyleNone;if( !_sscller.active ){cell.tuanGouModel = self.tuanGouArrM[indexPath.row];}else{ //搜尋結果cell.tuanGouModel = self.secArrM[indexPath.row];}return cell;}#pragma mark - TableV協議-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return ;}-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{[self.secArrM removeAllObjects];for (int j = ; j < _tuanGouArrM.count; j++) {TuanGouModel* model =[[TuanGouModel alloc]init];model = _tuanGouArrM[j];if ([model.title isEqualToString: _sscller.searchBar.text]) {[self.secArrM addObject: model];}}[self.myTable reloadData];}//允許Menu菜單-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{return YES;}//每個cell都可以點擊出現Menu菜單-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{return YES;}-(void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(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 *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{if (indexPath.row == self.tuanGouArrM.count - ) {NSLog(@"最後一行");TuanGouModel* model =[[TuanGouModel alloc]init];model = _tuanGouArrM[arcrandom()%];[_tuanGouArrM addObject:model];[self.myTable reloadData];}}//下拉重新整理-(void)setupRefresh{//.添加重新整理控制項UIRefreshControl *control=[[UIRefreshControl alloc]init];[control addTarget:self action:@selector(refreshStateChange:) forControlEvents:UIControlEventValueChanged];[self.myTable addSubview:control];//.馬上進入重新整理狀態,並不會觸發UIControlEventValueChanged事件[control beginRefreshing];// .載入資料[self refreshStateChange:control];}/*** UIRefreshControl進入重新整理狀態:載入最新的資料*/-(void)refreshStateChange:(UIRefreshControl *)control{TuanGouModel* model =[[TuanGouModel alloc]init];model = _tuanGouArrM[arcrandom()%];[_tuanGouArrM insertObject:model atIndex:];[self.myTable reloadData];NSLog(@"第一行");[control endRefreshing];}//指示是否允許高亮顯示選中的行- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{return YES;}//選中某行時執行- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{NSLog(@"selected: %ld, row:%ld", indexPath.section, indexPath.row);}//取消選中時執行,這個方法常在表格允許多選時調用執行- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{NSLog(@"Deselected: %ld, row:%ld", indexPath.section, indexPath.row);}

以上代碼是hi小編給大家介紹的iOS開發之UITableView與UISearchController實現搜尋及上拉載入,下拉重新整理執行個體代碼,希望對大家有所協助!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.