源文件這裡有http://pan.baidu.com/s/1pLlDm6f 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搜索及上拉載入,下拉刷新