概述
主要是代理中的协议函数,用来完成UITableView的构建。
示例
先看图
示例代码
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property(retain,nonatomic) UIWindow* window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//下面是添加导航栏和设置根视图控制器
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController* vc = [ViewController new];
UINavigationController* navC = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = navC;
[self.window makeKeyAndVisible];
return YES;
}
@end
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
//声明UITableView
UITableView* _tabView;
//数据源
NSMutableArray* _arrayData;
//导航栏中三个按钮
UIBarButtonItem* _btnEdit;
UIBarButtonItem* _btnFinish;
UIBarButtonItem* _btnDelate;
BOOL _isEdit;
}
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化UITableView
_tabView = [UITableView new];
//设置尺寸为屏幕尺寸
_tabView.frame = [UIScreen mainScreen].bounds;
//设置表单子view的宽高为自动调整
_tabView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
//设置UITableView的代理为本视图控制器
_tabView.delegate = self;
//设置UITableView的数据代理为本视图控制器
_tabView.dataSource = self;
//设置头部view为空(不显示头部)
_tabView.tableHeaderView = nil;
//设置底部view为空(不显示)
_tabView.tableFooterView = nil;
[self.view addSubview:_tabView];
//初始化并填充数据源
_arrayData = [NSMutableArray new];
for (int i='A'; i<'Z'; i++) {
NSString* str = [[NSString alloc] initWithFormat:@"item%c",i];
[_arrayData addObject:str];
}
//重新加载_tabView
[_tabView reloadData];
[self createBtn];
}
-(void) createBtn{
_btnEdit = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(pressEdit)];
self.navigationItem.rightBarButtonItem = _btnEdit;
_btnFinish = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(pressFinish)];
_btnDelate = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(pressDelate)];
}
//进入编辑状态
-(void) pressEdit{
self.navigationItem.rightBarButtonItem = _btnFinish;
_isEdit = YES;
[_tabView setEditing:YES];
self.navigationItem.leftBarButtonItem = _btnDelate;
}
//进入完成状态
-(void) pressFinish{
self.navigationItem.rightBarButtonItem = _btnEdit;
_isEdit = NO;
[_tabView setEditing:NO];
self.navigationItem.leftBarButtonItem = nil;
}
-(void) pressDelate{
}
//设置编辑模式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//UITableViewCellEditingStyleInsert:插入
//UITableViewCellEditingStyleDelete:删除
//UITableViewCellEditingStyleInsert|UITableViewCellEditingStyleDelete:多选
//UITableViewCellEditingStyleNone:无标示
return UITableViewCellEditingStyleDelete;
}
//进入编辑模式,当手指拖动单元格元素时候
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//删除数据源中数据
[_arrayData removeObjectAtIndex:indexPath.row];
//刷新列表
[_tabView reloadData];
}
//选中条目回调
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog([NSString stringWithFormat:@"selected----section=%d,row=%d",indexPath.section,indexPath.row]);
}
-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog([NSString stringWithFormat:@"deselected--section=%d,row=%d",indexPath.section,indexPath.row]);
}
//获取组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//获取每组中元素个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _arrayData.count;
}
//单元格(类似Android中ViewHolder)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* str = @"cell";
//从UITableView中尝试取出单元格对象
UITableViewCell* cell = [_tabView dequeueReusableCellWithIdentifier:str];
//如果取出的单元格元素为空,就创建一个
if (cell == nil) {
//注意UITableViewCellStyle,定义了cell中各个子view在cell中的位置以及子veiw的相对位置
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
}
//将单元格元素中title赋值
cell.textLabel.text = [_arrayData objectAtIndex:indexPath.row];
//设置子标题(只有当style为UITableViewCellStyleSubtitle,才会有子标题)
cell.detailTextLabel.text = @"子标题";
//图标
UIImage* image = [UIImage imageNamed:@"search.png"];
//UIImageView* iv = [[UIImageView alloc] initWithImage:image];
cell.imageView.image = image;
return cell;
}
//行高
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
@end