服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码

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

2021-01-16 14:58甘林梦 IOS

这篇文章主要介绍了iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码的相关资料,需要的朋友可以参考下

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

具体代码如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#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实现搜索及上拉加载,下拉刷新实例代码,希望对大家有所帮助!

延伸 · 阅读

精彩推荐
  • IOSiOS中MD5加密算法的介绍和使用

    iOS中MD5加密算法的介绍和使用

    MD5加密是最常用的加密方法之一,是从一段字符串中通过相应特征生成一段32位的数字字母混合码。对输入信息生成唯一的128位散列值(32个字符)。这篇文...

    LYSNote5432021-02-04
  • IOSiOS自定义UICollectionViewFlowLayout实现图片浏览效果

    iOS自定义UICollectionViewFlowLayout实现图片浏览效果

    这篇文章主要介绍了iOS自定义UICollectionViewFlowLayout实现图片浏览效果的相关资料,需要的朋友可以参考下...

    jiangamh8882021-01-11
  • IOSiOS实现控制屏幕常亮不变暗的方法示例

    iOS实现控制屏幕常亮不变暗的方法示例

    最近在工作中遇到了要将iOS屏幕保持常亮的需求,所以下面这篇文章主要给大家介绍了关于利用iOS如何实现控制屏幕常亮不变暗的方法,文中给出了详细的...

    随风13332021-04-02
  • IOS详解iOS中多个网络请求的同步问题总结

    详解iOS中多个网络请求的同步问题总结

    这篇文章主要介绍了详解iOS中多个网络请求的同步问题总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    liang199111302021-03-15
  • IOSiOS中滑动控制屏幕亮度和系统音量(附加AVAudioPlayer基本用法和Masonry简单使用)

    iOS中滑动控制屏幕亮度和系统音量(附加AVAudioPlayer基本用法和

    这篇文章主要介绍了iOS中滑动控制屏幕亮度和系统音量(附加AVAudioPlayer基本用法和Masonry简单使用)的相关资料,需要的朋友可以参考下...

    CodingFire13652021-02-26
  • IOSiOS中UILabel实现长按复制功能实例代码

    iOS中UILabel实现长按复制功能实例代码

    在iOS开发过程中,有时候会用到UILabel展示的内容,那么就设计到点击UILabel复制它上面展示的内容的功能,也就是Label长按复制功能,下面这篇文章主要给大...

    devilx12792021-04-02
  • IOSiOS开发技巧之状态栏字体颜色的设置方法

    iOS开发技巧之状态栏字体颜色的设置方法

    有时候我们需要根据不同的背景修改状态栏字体的颜色,下面这篇文章主要给大家介绍了关于iOS开发技巧之状态栏字体颜色的设置方法,文中通过示例代码...

    梦想家-mxj8922021-05-10
  • IOSiOS开发之视图切换

    iOS开发之视图切换

    在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单。在iOS开发中常用的视图切换有三种,今天我们将...

    执着丶执念5272021-01-16