欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-28 23:10:52
废话不多说了,直接给大家贴代码了。 具体代码如下所示: #import "viewcontroller.h" #import "tuangoumodel.h"...

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

具体代码如下所示:

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