最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS開(kāi)發(fā)之UITableView與UISearchController實(shí)現(xiàn)搜索及上拉加載,下拉刷新實(shí)例代碼

 更新時(shí)間:2016年04月20日 09:37:17   作者:甘林夢(mèng)  
這篇文章主要介紹了iOS開(kāi)發(fā)之UITableView與UISearchController實(shí)現(xiàn)搜索及上拉加載,下拉刷新實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下

廢話不多說(shuō)了,直接給大家貼代碼了。

具體代碼如下所示:

#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 - 導(dǎo)航
-(void)createNa{
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(tableEdit:)];
self.navigationItem.rightBarButtonItem = rightItem;
self.title = @"美食家";
}
// 點(diǎn)擊導(dǎo)航右側(cè)編輯按鈕時(shí),讓表格可編輯
-(void)tableEdit:(UIBarButtonItem *) btnItem{
// if (self.myTable.editing == NO ) { // 沒(méi)有處于編輯狀態(tài),導(dǎo)航按鈕文字為“Edit”
// // 點(diǎn)擊“編輯”文字,讓表格處于編輯狀態(tài),并把按鈕的文字修改為“Done"
// self.myTable.editing = YES;
// 
// }else{
// // 編輯狀態(tài)下,點(diǎn)擊”Done"按鈕,取消表格的編輯狀態(tài),修改導(dǎo)航按鈕文字為"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 - 團(tuán)購(gòu)數(shù)據(jù)懶加載
-(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 - 數(shù)據(jù)源協(xié)議
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ( _sscller.active ) { //搜索結(jié)果表格
return self.secArrM.count;
}
else{
return self.tuanGouArrM.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//注冊(cè)
[tableView registerClass:[TuanGouTableViewCell class] forCellReuseIdentifier:@"tuanCell"];
//重置
TuanGouTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"tuanCell"forIndexPath:indexPath];
cell.backgroundColor = [UIColor yellowColor];
// 選中風(fēng)格
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if( !_sscller.active ){
cell.tuanGouModel = self.tuanGouArrM[indexPath.row];
}else{ //搜索結(jié)果
cell.tuanGouModel = self.secArrM[indexPath.row];
}
return cell;
}
#pragma mark - TableV協(xié)議
-(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;
}
//每個(gè)cell都可以點(diǎn)擊出現(xiàn)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(@"長(zhǎng)按");
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];
//.馬上進(jìn)入刷新?tīng)顟B(tài),并不會(huì)觸發(fā)UIControlEventValueChanged事件
[control beginRefreshing];
// .加載數(shù)據(jù)
[self refreshStateChange:control];
}
/**
* UIRefreshControl進(jìn)入刷新?tīng)顟B(tài):加載最新的數(shù)據(jù)
*/
-(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;
}
//選中某行時(shí)執(zhí)行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"selected: %ld, row:%ld", indexPath.section, indexPath.row);
}
//取消選中時(shí)執(zhí)行,這個(gè)方法常在表格允許多選時(shí)調(diào)用執(zhí)行
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"Deselected: %ld, row:%ld", indexPath.section, indexPath.row);
}

以上代碼是hi小編給大家介紹的iOS開(kāi)發(fā)之UITableView與UISearchController實(shí)現(xiàn)搜索及上拉加載,下拉刷新實(shí)例代碼,希望對(duì)大家有所幫助!

相關(guān)文章

最新評(píng)論

武夷山市| 镇雄县| 东辽县| 民和| 龙泉市| 高邮市| 福清市| 清远市| 兴仁县| 舞钢市| 鄂温| 奉贤区| 辉南县| 黄骅市| 泾源县| 金乡县| 平阴县| 定边县| 时尚| 井研县| 诸城市| 顺平县| 东兰县| 宽城| 开化县| 苍溪县| 马关县| 闵行区| 尼木县| 浑源县| 五台县| 宝应县| 噶尔县| 枝江市| 灵宝市| 凌海市| 乌审旗| 卓资县| 六盘水市| 离岛区| 南涧|