废话不多说了,直接给大家贴代码了。

具体代码如下所示:

#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 ()

{

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实现搜索及上拉加载,下拉刷新实例代码,希望对大家有所帮助!

tableview插入刷新_iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码...相关推荐

  1. ios 上拉加载 下拉刷新

    在一款 App应用中有的时候会用到上拉加载下拉刷新的功能,本人觉得SVPullToRefresh很好用(可以用在UIScrollView上,包括UITableview和UICollectionView ...

  2. php微信小程序向下滑动,微信小程序功能实现:上滑加载下拉刷新

    本篇文章给大家带来的内容是关于微信小程序功能实现:上滑加载下拉刷新,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 之前谈到文章列表的数据加载,是一次性全部加载,这样是不友好的.这章介 ...

  3. ionic上拉加载-下拉刷新

    ionic上拉加载-下拉刷新 1.上拉加载 <ion-infinite-scroll on-infinite="loadOlderStories()" distance=&q ...

  4. Mint-ui中loadmore(上拉加载下拉刷新)组件在ios中滑动会触发点击事件的解决方法...

    bug说明: Mint-ui中loadmore(上拉加载下拉刷新)组件 在 使用fastclick的情况下 ,在ios设备中滑动会触发点击事件: 解决方法: 我是按需引入,去项目中找到loadmore ...

  5. php mescroll,mescroll.js上拉加载下拉刷新组件使用详解

    本文实例为大家分享了上拉加载下拉刷新组件mescroll.js的具体代码,供大家参考,具体内容如下 使用注意事项: 1.引入的时候出问题及时看官方给出的解决方案(基本上都必须看): 2.react中一 ...

  6. 教你如何使用SwipeRefreshLayout来构建一个上拉加载下拉刷新框架

    前言: 基本上所以的移动端应用都有Listview(当然RecyclerView也一样),那必不可少的都会嵌入一个上拉加载下拉刷新的功能.这样能大大的减少用户的流量消耗,同样对于用户也有更好的用户体验 ...

  7. jquery实现上拉加载下拉刷新

    引入dropload插件 需要引入的文件 <script src="js/zepto.min.js"></script> <script src=&q ...

  8. 上拉加载下拉刷新了解下

    2019独角兽企业重金招聘Python工程师标准>>> 老样子,我们先,哦不,今天我们直接上思路,没有效果图,真的没有 我们依旧从界面及逻辑两块进行分析 1.界面上,只分成简单的两块 ...

  9. vue 实现上拉加载下拉刷新(思路贼清晰)

    项目需要用到上拉刷新下拉加载 所以自己手动实现了一个 组件: <template><div class="my-scroll" :class="[scr ...

最新文章

  1. SurfaceView 间取得焦点
  2. 主流Java报表工具的比较
  3. python怎么验证两个json是不是一样_比较Python中的两个JSON树
  4. 【C++】std::是什么?
  5. weblogic启动失败:Could not obtain the localhost address 解决办法
  6. WPS中如何启用宏,附wps.vba.exe下载链接(百度云盘,永久有效)
  7. Windows时间同步出错|无法获取服务器时间解决办法
  8. js pug 代码_Pug 代码 Code
  9. 测量电源纹波-正确测量方法
  10. Win10卸载预装软件最全教程
  11. 163邮箱登录入口你知道吗?163邮箱登录方法大全
  12. 哪款蓝牙耳机性价比最高?2023性价比高的蓝牙耳机推荐
  13. 数据分析(1):对比分析法
  14. 28388-连接管理器 (CM)
  15. Word合并所有段落再按字数划分段落
  16. 九章量子计算机应用,“九章”量子计算机这么牛,到底有什么用? |【经纬低调分享】...
  17. pn532复制教程 linux,Linux下pn532板子测试学校水卡
  18. 快速使用Android串口
  19. java榨汁机榨取不同水果,榨汁机别乱买,亲测榨水果翻车,九阳迷你原汁机评测...
  20. R语言中的attach()与detach()

热门文章

  1. python 水位_Leetcode 42. 接雨水 - python - 递归 查找分水岭
  2. 用c语言编程计算10,计算方法c语言编程.doc
  3. 全国计算机一级msoffice考试内容,2015年全国计算机一级MSOffice考试大纲
  4. python怎么输入三个数按大小输出_Python练习一 : 随机输入三数字,按大小顺序输出...
  5. python模拟购物模块_python -模拟购物 -赊账版 粗糙之极
  6. python两列相乘_如何将pandas中具有不同索引的两列相乘?
  7. api-ms-win-crt-runtime-l1-1-0.dll和api-ms-win-downlevel-shlwapi-l1-1-0.dll免费下载
  8. python3.6怎么安装numpy_Python3.6_安装numpy(2)
  9. iframe内容适应div大小_使用lt;iframegt;方式在WordPress中插入视频并自适应屏幕尺寸
  10. extjs html页面刷新,ExtJS onReady引发的浏览器对页面的加载过程