UITableView and UISearchController search and pull-up loading, pull-down refresh, and uitableview pull-down refresh
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 * _ sscroller; 9} 10 @ property (nonatomic, strong) NSMutableArra Y * 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 = @ "Gourmet"; 29} 30 31 # pr Agma mark-Navigation 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 = @ "Gourmet"; 37 38} 39 40 // when you click the edit button on the right of the navigation bar, edit the table 41-(void) tableEdit :( UIBarButtonItem *) btnItem {42 43 // if (self. myTable. editing = NO ){/ /The text of the navigation button is "Edit" 44 // click "Edit" to make the table editable, and change the text of the button to "Done" 45 // self. myTable. editing = YES; 46 // 47 //} else {48 /// in the editing status, click the "Done" button to cancel the table editing status, modify the text of the navigation button to "Edit" 49 // self. myTable. editing = NO; 50 // btnItem. title = @ "Edit"; 51 // self. navigationItem. rightBarButtonItems = @ [btnItem]; 52 //} 53 54} 55 56-(void) createsecB {57 _ sscroller = [[UISearchController alloc] initWithSearchResu LtsController: nil]; 58 _ sscroller. searchResultsUpdater = self; 59 self. myTable. tableHeaderView = _ sscroller. 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 didreceivemorywarning]; 80 81} 82 # pragma mark-Table lazy loading 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-group buying data lazy loading 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 [_ tu AnGouArrM addObject: model]; 108} 109} 110 return _ tuanGouArrM; 111} 112 113 # pragma mark-Data Source protocol 114-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {115 if (_ sscroller. active) {// search result Table 116 return self. secArrM. count; 117} 118 119 else {120 return self. tuanGouArrM. count; 121 122} 123 124-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexP Ath :( NSIndexPath *) indexPath {126 127 // register 128 [tableView registerClass: [TuanGouTableViewCell class] forCellReuseIdentifier: @ "tuanCell"]; 129 // reset 130 TuanGouTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: @ "tuanCell" forIndexPath: indexPath]; 131 cell. backgroundColor = [UIColor yellowColor]; 132 // selected style 133 cell. selectionStyle = UITableViewCellSelectionStyleNone; 134 if (! _ Sscroller. active) {135 cell. tuanGouModel = self. tuanGouArrM [indexPath. row]; 136} else {// search result 137 cell. tuanGouModel = self. secArrM [indexPath. row]; 138} 139 140 141 return cell; 142} 143 # pragma mark-TableV protocol 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 isw.tostring: _ sscroller. searchBar. text]) {157 [self. secArrM addObject: model]; 158} 159} 160 161 [self. myTable reloadData]; 162} 163 164 165 // allow Menu 166-(BOOL) tableView :( UITableView *) tableView shouldShowMenuForRowAtIndexPath :( NSIndexPath *) indexPath167 {168 return YES; 169} 170 171 // each cell can be clicked to display Menu 172-(BOOL) tableView :( UITableView *) tableView can1_maction :( SEL) action forRowAtIndexPath :( NSIndexPath *) indexPath withSender :( id) sender173 {174 return YES; 175 176} 177-(void) tableView :( UITableView *) tableView upload maction :( SEL) action forRowAtIndexPath :( NSIndexPath *) indexPath withSender :( id) sender {178 179 NSLog (@ "Long press"); 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 // pull up to load 206-(void) tableView :( UITableView *) tableView willDisplayCell :( UITableViewCell *) cell forRowAtIndexPath :( NSIndexPath *) indexPath {207 if (indexPath. row = self. tuanGouArrM. count-1) {208 NSLog (@ "last line"); 209 210 TuanGouModel * model = [[TuanGouModel alloc] init]; 211 model = _ tuanGouArrM [arc4random () % 12]; 212 [_ tuanGouArrM addObject: model]; 213 [self. myTable reloadData]; 214 215} 216 217 // pull-down refresh 218-(void) setupRefresh220 {219 // 1. add refresh control 222 UIRefreshControl * control = [[UIRefreshControl alloc] init]; 223 [control addTarget: self action: @ selector (refreshStateChange :) forControlEvents: UIControlEventValueChanged]; 224 [self. myTable addSubview: control]; 225 226 // 2. refreshing immediately does not trigger the UIControlEventValueChanged event 227 [control beginRefreshing]; 228 229 // 3. load data 230 [self refreshStateChange: control]; 231} 232 233 234/** 235 * UIRefreshControl enters the refresh status: load the latest data 236 */237-(void) refreshStateChange :( UIRefreshControl *) control238 {239 callback * model = [[callback alloc] init]; 240 model = _ tuanGouArrM [arc4random () % 12]; 241 [_ tuanGouArrM insertObject: model atIndex: 0]; 242 [self. myTable reloadData]; 243 244 NSLog (@ "first line"); 245 [control endRefreshing]; 246 247} 248 249 // indicates whether the selected row 250-(BOOL) tableView (UITableView *) tableView shouldHighlightRowAtIndexPath :( NSIndexPath *) indexPath {251 252 return YES; 253} 254 255 // execute 256-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {257 NSLog (@ "selected: % ld, row: % ld ", indexPath. section, indexPath. row); 258} 259 // This method is executed when multiple selections are allowed in the table. This method is called to execute 260-(void) tableView :( UITableView *) tableView didDeselectRowAtIndexPath :( NSIndexPath *) indexPath {261 NSLog (@ "Deselected: % ld, row: % ld", indexPath. section, indexPath. row); 262}
The source file has a http://pan.baidu.com/s/1pLlDm6f.
UITableView and UISearchController search and pull-up loading, pull-down refresh