美文网首页
IOS开发 UITableView单元格

IOS开发 UITableView单元格

作者: 奔跑的小小鱼 | 来源:发表于2017-06-07 21:59 被阅读25次

UITableView单元格

本节学习内容:

1.UITableView单元格基础

2.UITableView单元格的定义

3.UITableView单元格的使用

UITableView单元格的属性

UITableViewCeell:数据试图单元格类型

dequeueReusableCellWithIdentifier:获取可以复用的单元格对象

initWithStyle:根据峁格创建单元格对象

reuseldentifier:设置可以复用单元格的ID

【ViewController.h】

#import<UIKit/UIKit>

@interface ViewController:UIViewController{

//数据视图

UITableView* _tableView;

//数据源

NSMutableArray* _arrayData;

//添加导航按钮

UIBarButtonItem* _btnEdit;

UIBarButtonItem* _btnFinish;

UIBarButtonItem* _btnDelete;

//设置编辑状态

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];

//自动调整子视图的大小

_tableView.autoresizingMask=UIViewAutoresizingFlexBleHeight]UIViewAutoresizingFlexibleWidth;

//设置代理

_tableView.delegate=self;

_tableView.dataSorce=self;

//数据视图的头部视图的设定

_tableView.tableHeaderView=nil;

//数据视图的尾部视图的设定

_tableView.tableFooterView=nil;

[self.view addSubview:_tableView];

//初始化数据源数组

_arrayData=[[NSMutableArray alloc]init];

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

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

[_arrayData addObject:str];

}

//当数据的数据源发生变化时更新数据视图,从新加载数据

[_arrayData reloadData];

}

-(void)createBtn{

//创建功能按钮

_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;

//导航栏右侧按钮置为完成

self.navigationItem.rightBarButtonItem=_btnFinish;

[_tableView setEditing:YES];

//导航栏左侧按钮置为删除

self.navingationItem.leftBarButtonItem=_btnDelete;

}

-(void)pressFinish{

_isEdit=NO;

//导航栏右侧按钮置为完成

self.navigationItem.rightBarButtonItem=_btnEdit;

[_tableView setEditing:NO];

//导航栏左侧按钮为空

self.navigationItem.leftBarButtonItem=_nil;

}

//可以显示编辑状态,当手指在单元格上移动时

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRow|AtIndexPath:(NSIndexPath *)indexPath{

//删除数据源对应的数据

[_arrayData removeObjectAtIndex:Path.row];

//数据源更新

[_tableView reloadData]

}

//选中单元格调用此协议

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSLog(@"选中单元格!%d,%d",indexPath.section,indexPath.row);

}

//取消单元格谳用的协议函数

-(void)tableView:(UITableView*)tableView didDesclectRowAtIndexPath:(NSIndexPath *)indexPath{

NSLog(@"取消单元格!%d,%d",indexPath.section,indexPath.row);

}

//单元格显示效果协议

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

//UITableViewCellEditingStyleNone 空白

//UITableViewCellEditingStyleInsert 添加

//UITableViewCellEditingStyleDelete 删除,默认为删除

//UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleIInsert;删除添加组状态(多选状态)

returen UITableViewCellEditingStyleDelete;

}

-(NSInteger)tabview:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

returen _arrayData.coun;

}

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

NSString* strID=@"ID";

/.尝试获取可以复用的单元格,如果得不到,返回Nil

UITableViewCell* cell=[_tableView dequeReusableCellWithIdentifier:strID];

if(cell == nil){

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

}

//单元格文字赋值

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

//设置子标题

cell.detailTextLable.text=@"子标题";

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

UIImage* image=[UIImage imageNamed:str];

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

//设置默认图标信息

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;

}

相关文章

网友评论

      本文标题:IOS开发 UITableView单元格

      本文链接:https://www.haomeiwen.com/subject/uzsnfxtx.html