iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码_IOS

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

具体代码如下所示:

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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索uitableview下拉刷新
uisearchcontroller
iscroll5下拉刷新实例、mui下拉刷新实例、uisearchcontroller、uiviewcontroller刷新、viewcontroller刷新,以便于您获取更多的相关知识。

时间: 2024-08-29 09:17:03

iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码_IOS的相关文章

iOS开发之UITableView左滑删除等自定义功能_IOS

前言 相信每位iOS开发者都知道UITableView的左滑删除功能非常的炫酷,有时候左滑需要的功能不止只有删除一个,有时候会有顶置之类的别的功能,这时候就需要我们自己定制左滑 示例代码 -(NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *rowActio

iOS开发之UITableView详解_IOS

一.UITableView基本介绍 默认的UITableView有2种风格: UITableViewStylePlain(不分组) UITableViewStyleGrouped(分组) UITableView中的数据只有行的概念,没有列的概念,UITableView的每行数据就是一个UITableViewCell. 自带的UITableViewCell的类型选择有: 复制代码 代码如下: typedef NS_ENUM(NSInteger, UITableViewCellStyle) {   

iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键_IOS

最近做一个搜索用户的功能,这里使用了UISearchBar.由于搜索的方式只有手机号码,所以这里的键盘要限制为数字输入,可以这么做: self.searchBar.keyboardType = UIKeyboardTypeNumberPad;如果使用的不是搜索框而是textField输入框,可以设置textField的键盘属性来展示 self.textField.keyboardType = UIKeyboardTypeNumberPad;监听事件如下所示即可. 但是这里有个问题,就是数字键盘上

Swift开发之UITableView状态切换效果_Swift

效果 源码 https://github.com/YouXianMing/Swift-Animations // // TableViewTapAnimationController.swift // Swift-Animations // // Created by YouXianMing on 16/8/7. // Copyright 2016年 YouXianMing. All rights reserved. // import UIKit class TableViewTapAnima

iOS开发之SQLite--C语言接口规范(五)——iOS开发使用SQLite实例

本篇博客就使用前面操作SQLite的知识来实现如何去插入,删除和更新数据.然后再把操作SQlite数据库常用的方法进行一个封装.把常用方法进行封装后,把Cars数据库中的其中一个表的数据进行查询,并在UITableView上进行展示.因为本实例要对数据库的数据进行modify(修改)操作 ,在iOS系统上呢,为了安全起见,在Bundle中的数据库资源是不允许进行数据的插入修改和删除操作的.在之前的博客中我们只进行了查询操作,所以从Bundle加载数据库资源文件是可行的. 如果对数据库进程inse

iOS开发之MJRefresh

1.概述 MJRefresh是iOS开发中最常用的开源框架之一,主要用于UITanleView的下拉.上拉刷新.本框架兼容iOS6\iOS7,iPhone\iPad横竖屏,目前大多数商业app中都用到了这个框架. 框架地址:https://github.com/CoderMJLee/MJRefresh 2.使用方法 (1)下载框架,导入头文件 ? 1 #import "MJRefresh.h" (2)添加头部控件的方法 ? 1 [self.tableView addHeaderWith

iOS开发-ios7下拉刷新,上提加载快速集成

在ios7之前,一直在使用开源的EGO库.但是,在使用过程中发现,普遍封装得过于复杂.耦合性强,不利于集成到自己的项目中. 另外,在ios7之后,一些原有的下拉刷新,上提加载控件表现的就不是那么出色了.除了可能出错外,也不符合扁平化的风格. 后来,在code4App上发现了一个大牛上传了一个开源代码, 仅需几行代码就可以为UITableView或者CollectionView加上下拉刷新或者上拉刷新功能.可以自定义上下拉刷新的文字说明. 下载下来自己试了下,发现不错.  mark下. 下载链接:

举例讲解iOS中延迟加载和上拉刷新/下拉加载的实现_IOS

lazy懒加载(延迟加载)UITableView 举个例子,当我们在用网易新闻App时,看着那么多的新闻,并不是所有的都是我们感兴趣的,有的时候我们只是很快的滑过,想要快速的略过不喜欢的内容,但是只要滑动经过了,图片就开始加载了,这样用户体验就不太好,而且浪费内存.              这个时候,我们就可以利用lazy加载技术,当界面滑动或者滑动减速的时候,都不进行图片加载,只有当用户不再滑动并且减速效果停止的时候,才进行加载.               刚开始我异步加载图片利用SDWe

iOS 3.0下拉加载更多

问题描述 - (NSArray *)loadMoreMessagesFromId:(NSString *)aMessageId limit:(int)aLimit; 现在ios只有个方法嘛? 我怎么下拉加载更多呢?没有了2.0的时间戳的方法...越拉 limit数组越大, 然后剔除已经存在的数据么? 解决方案 初始加载20条NSArray *messages = [conversation loadMoreMessagesFromId:nil limit:20];保存数组中最早的一条记录firs