#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[arc4random()%];
[_tuanGouArrM addObject:model];
[self.myTable reloadData]; }
} //下拉刷新
-(void)setupRefresh
{
//1.添加刷新控件
UIRefreshControl *control=[[UIRefreshControl alloc]init];
[control addTarget:self action:@selector(refreshStateChange:) forControlEvents:UIControlEventValueChanged];
[self.myTable addSubview:control]; //2.马上进入刷新状态,并不会触发UIControlEventValueChanged事件
[control beginRefreshing]; // 3.加载数据
[self refreshStateChange:control];
} /**
* UIRefreshControl进入刷新状态:加载最新的数据
*/
-(void)refreshStateChange:(UIRefreshControl *)control
{
TuanGouModel* model =[[TuanGouModel alloc]init];
model = _tuanGouArrM[arc4random()%];
[_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);
}

源文件这里有http://pan.baidu.com/s/1pLlDm6f

UITableView与UISearchController搜索及上拉加载,下拉刷新

UITableView与UISearchController搜索及上拉加载,下拉刷新的更多相关文章

  1. 上拉加载下拉刷新控件WaterRefreshLoadMoreView

    上拉加载下拉刷新控件WaterRefreshLoadMoreView 效果: 源码: // // SRSlimeView // @author SR // Modified by JunHan on ...

  2. Vue mint ui用在消息页面上拉加载下拉刷新loadmore 标记

    之前总结过一个页面存在多个下拉加载的处理方式,今天再来说一下在消息页面的上拉加载和下拉刷新,基本上每个app都会有消息页面,会遇到这个需求 需求:每次加载十条数据,上拉加载下拉刷新,并且没有点击查看过 ...

  3. RecyclerView 上拉加载下拉刷新

    RecyclerView 上拉加载下拉刷新 <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/teach_s ...

  4. APICloud上啦加载下拉刷新模块

    apicloud有自带的上啦加载下拉刷新,当让也可以用第三方或者在模块库里面找一个使用 一.下拉刷新,一下代码写在 apiready = function (){} 里面 apiready = fun ...

  5. 微信小程序上拉加载下拉刷新

    微信小程序实现上拉加载下拉刷新 使用小程序默认提供方法. (1). 在xxx.json 中开启下拉刷新,需要设置backgroundColor,或者是backgroundTextStyle ,因为加载 ...

  6. mui scroll和上拉加载/下拉刷新

    mui中 scroll和上拉加载/下拉刷新同时存在会出现两个滚动条 把/*   */ /* //mui页面鼠标拖动代码: mui('.mui-scroll-wrapper').scroll({ dec ...

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

    package com.example.swiperefreshlayoutdemo; import java.util.ArrayList;import java.util.HashMap; imp ...

  8. zepto.js + iscroll.js上拉加载 下拉加载的 移动端 新闻列表页面

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  9. MJRefresh(上拉加载下拉刷新)

    整理自:https://github.com/CoderMJLee/MJRefresh#%E6%94%AF%E6%8C%81%E5%93%AA%E4%BA%9B%E6%8E%A7%E4%BB%B6%E ...

  10. Flutter上拉加载下拉刷新---flutter_easyrefresh

    前言 Flutter默认不支持上拉加载,下拉刷新也仅仅支持Material的一种样式.Android开发使用过SmartRefreshLayout的小伙伴都知道这是一个强大的刷新UI库,集成了很多出色 ...

随机推荐

  1. 【转】jQuery中.bind() .live() .delegate() .on()的区别

    bind(type,[data],fn) 为每个匹配元素的特定事件绑定事件处理函数 $("a").bind("click",function(){alert(& ...

  2. 代码生成工具Database2Sharp中增加视图的代码生成以及主从表界面生成功能

    在代码生成工具的各种功能规划中,我们一向以客户的需求作为驱动,因此也会根据需要增加一些特殊的功能或者处理.在实际的开发中,虽然我们一般以具体的表进行具体业务开发,但是有些客户提出有时候视图开发也是很常 ...

  3. js隐藏或显示某区域

    隐藏: document.getElementById(“keleyi”).style.display = “none”; 显示: document.getElementById(“keleyi”). ...

  4. 常用的一些SQL语句整理,也许有你想要的。

    本篇文章是对一些常用的sql语句进行了总结与分析,需要的朋友参考下,也许会有你需要的. 1.SQL行列转换 问题:假设有张学生成绩表(tb)如下:姓名 课程 分数张三 语文 74张三 数学 83张三 ...

  5. QTableWidget 使用及美化_QtableWidget_QtableView滚动条宽度及样式

      //创建及属性设置m_tableWidget = new QTableWidget(this);m_tableWidget->setRowCount(10);m_tableWidget-&g ...

  6. 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)

    [源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成 ...

  7. jquery简单原则器(匹配第一个元素)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. JVM简介

    关于java的JVM这块儿知识,在项目做大之后,一些性能的优化,要涉及到数据库,一些缓存要放在内存中.还有一些JMS的消息传播等等,高大上的知识需要有JVM内存模型知识的支持.所以自问自答,来回答下面 ...

  9. Eclipse功能集合

    大家好,这篇博客的目的是总结一下Eclipse这个软件中一些不为常用的功能.与大家分享.谢谢~ 1.利用one hour看了一下Eclipse的使用,用two hour写了这篇blog. 2.在现实项 ...

  10. Mysql优化的几点总结

    正常情况下,初创公司的流量并不是很大,mysql数据库在未做优化的情况依然可以满足性能要求,特别是5.6版本后mysql在性能上还是有了很大提升,所以在初期并没有花精力在此上面.但后来发生的一系列问题 ...