IOS開發(fā) UITableView高級協(xié)議

本節(jié)學(xué)習(xí)內(nèi)容:

1.UITableView高協(xié)議介紹

2.UITableView高級協(xié)議的功能

3.UITableView高級協(xié)議的使用

commitEditingStyle:提交編輯函數(shù)

canEditRowAtIndexPath:開啟關(guān)閉編輯單元格

editing|StyleForRowAtIndexPath:編輯單元格峁格設(shè)定

didSelectRowAtIndexPath:選中單元格響應(yīng)協(xié)議

didDeselectRowAtIndexPath:返選單元格響應(yīng)協(xié)議



【ViewController.h】

#import<UIKit/UIKit.h>

@interface ViewController:UIViewController<UITableViewDataSource,UITableViewDelegate>{

//數(shù)據(jù)視圖

UITableView* _tableView;

//數(shù)據(jù)源

NSMutableArray* _arrayData;

//添加導(dǎo)航按鈕

UIBarButtonItem* _btnEdit;

UIBarButtonItem* _btnFinish;

UIBarButtonItem* _btnDelete;

//設(shè)置編輯狀態(tài)

BOOL _isEdit;

}

【ViewController.m】

#import "viewController.h"

@interface Viewcontroller()

@end

@implementation viewController

-(void)viewDidLoad{

[super viewDidLoad]

_tableView=[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

//自動調(diào)整子視圖的大小

_tableView.autoresizingMask=UIViewAutoresizingFlexBleHeight]UIViewAutoresizingFlexibleWidth;

//設(shè)置代理

_tableView.delegate=self;

_tableView.dataSorce=self;

//數(shù)據(jù)視圖的頭部視圖的設(shè)定

_tableView.tableHeaderView=nil;

//數(shù)據(jù)視圖的尾部視圖的設(shè)定

_tableView.tableFooterView=nil;

[self.view addSubview:_tableView];

//初始化數(shù)據(jù)源數(shù)組

_arrayData=[[NSMutableArray alloc]init];

for(init i=1;i<20;I++){

NSString* str=[NSString stringWithFormat:@"A %d",i];

[_arrayData addObject:str];

}

//當(dāng)數(shù)據(jù)的數(shù)據(jù)源發(fā)生變化時更新數(shù)據(jù)視圖,從新加載數(shù)據(jù)

[_arrayData reloadData];

}

-(void)createBtn{

//創(chuàng)建功能按鈕

_isEdit=NO;

_btnEdit=[[UIBarButtonIten alloc] initWithTitle:@"編輯" style:UIBarButtonItemStylePlain target:self action:@selector(pressEdit)];

_btnFinish=[[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(pressFinish)];

_btnDelete=[[UIBarButtonItem alloc]initWithTitle:@"刪除" style:UIBarButtonItemStylePlain target:self action:@selector(pressDelete)];

slef.navigationItem.leftBarButtonItem=_btnEdit;

}

-(void)pressEdit{

_isEdit=YES;

//導(dǎo)航欄右側(cè)按鈕置為完成

self.navigationItem.rightBarButtonItem=_btnFinish;

[_tableView setEditing:YES];

//導(dǎo)航欄左側(cè)按鈕置為刪除

self.navingationItem.leftBarButtonItem=_btnDelete;

}

-(void)pressFinish{

_isEdit=NO;

//導(dǎo)航欄右側(cè)按鈕置為完成

self.navigationItem.rightBarButtonItem=_btnEdit;

[_tableView setEditing:NO];

//導(dǎo)航欄左側(cè)按鈕為空

self.navigationItem.leftBarButtonItem=_nil;

}


-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

NSString* strID=@"ID";

/.嘗試獲取可以復(fù)用的單元格,如果得不到,返回Nil

UITableViewCell* cell=[_tableView dequeReusableCellWithIdentifier:strID];

if(cell == nil){

cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strID];

}

//單元格文字賦值

cell.textLable.text=[_arrayDate ObjectAtIndex:indexPath.row];

//設(shè)置子標(biāo)題

cell.detailTextLable.text=@"子標(biāo)題";

NSString* str=[NSString stringWithFormat:@"%ld.png",indexPath.row%7+1];

UIImage* image=[UIImage imageNamed:str];

//UIImageView iView=[[UIImageView alloc]initWithImage:image];

//設(shè)置默認(rèn)圖標(biāo)信息

cell.imageView.image=image;

returen cell;

}

//單元格高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 60;

}


【AppDelegate.m】

#import "AppDelegate.h"

#import "ViewController.h"

@interface AppDelegate()

@end

@implementation AppDelegate

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

self.window=[[UIWindow alloc] initWithFrame:[UIScreen aminScreen].bounds];

UINavigationController* nav=[[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];

self.window.rootViewController=nav;

return YES;

}


效果圖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容