iOS development of UITableView and UISearchController search and pull up load pull down refresh instance code

  • 2020-05-30 21:07:36
  • OfStack

Without further ado, I'm just going to post the code.

The specific code is as follows:


#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 = @" The gourmet ";
}
#pragma mark -  navigation 
-(void)createNa{
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(tableEdit:)];
self.navigationItem.rightBarButtonItem = rightItem;
self.title = @" The gourmet ";
}
//  Click the edit button on the right side of the navigation to make the table editable 
-(void)tableEdit:(UIBarButtonItem *) btnItem{
// if (self.myTable.editing == NO ) { //  Not in edit state, navigation button text is" Edit " 
// //  Click "edit" to make the table in the edit state and change the text of the button to" Done"
// self.myTable.editing = YES;
// 
// }else{
// //  In edit mode, click" Done" Button to cancel the editing state of the form and modify the text of the navigation button as "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 -  Lazy table loading 
-(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 -  Group purchase data lazy load 
-(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 -  Data source protocol 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ( _sscller.active ) { // Search results table 
return self.secArrM.count;
}
else{
return self.tuanGouArrM.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// registered 
[tableView registerClass:[TuanGouTableViewCell class] forCellReuseIdentifier:@"tuanCell"];
// reset 
TuanGouTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"tuanCell"forIndexPath:indexPath];
cell.backgroundColor = [UIColor yellowColor];
//  Select the style 
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if( !_sscller.active ){
cell.tuanGouModel = self.tuanGouArrM[indexPath.row];
}else{ // The search results 
cell.tuanGouModel = self.secArrM[indexPath.row];
}
return cell;
}
#pragma mark - TableV agreement 
-(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];
}
// allow Menu The menu 
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// each cell They can all be clicked Menu The 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(@" Long press ");
if (action ==@selector(copy:)) {
NSLog(@"copy");
}
if (action ==@selector(cut:)) {
NSLog(@"cut");
}
if (action ==@selector(paste:)) {
NSLog(@"paste");
}
if (action ==@selector(selectAll:)) {
NSLog(@"selectAll");
}
}
// Pull on loading 
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == self.tuanGouArrM.count - ) {
NSLog(@" The last 1 line ");
TuanGouModel* model =[[TuanGouModel alloc]init];
model = _tuanGouArrM[arcrandom()%];
[_tuanGouArrM addObject:model];
[self.myTable reloadData];
}
}
// The drop-down refresh 
-(void)setupRefresh
{
//. Add a refresh control 
UIRefreshControl *control=[[UIRefreshControl alloc]init];
[control addTarget:self action:@selector(refreshStateChange:) forControlEvents:UIControlEventValueChanged];
[self.myTable addSubview:control];
//. Enter the refresh state immediately without triggering UIControlEventValueChanged The event 
[control beginRefreshing];
// . Load the data 
[self refreshStateChange:control];
}
/**
* UIRefreshControl Enter the refresh state: the latest data is loaded 
*/
-(void)refreshStateChange:(UIRefreshControl *)control
{
TuanGouModel* model =[[TuanGouModel alloc]init];
model = _tuanGouArrM[arcrandom()%];
[_tuanGouArrM insertObject:model atIndex:];
[self.myTable reloadData];
NSLog(@" The first 1 line ");
[control endRefreshing];
}
// Indicates whether highlighting the selected line is allowed 
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
// Executes when a row is selected 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"selected: %ld, row:%ld", indexPath.section, indexPath.row);
}
// This method is often invoked when the table allows multiple selections 
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"Deselected: %ld, row:%ld", indexPath.section, indexPath.row);
}

The above code is hi this site to introduce the iOS development of UITableView and UISearchController search and pull up load, pull down refresh instance code, hope to help you!


Related articles: