IOS开发 UITableView单元格

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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,254评论 3 38
  • { 24、Sqlite数据库 1、存储大数据量,增删改查,常见管理系统:Oracle、MSSQLServer、DB...
    CYC666阅读 1,041评论 0 1
  • 课程要点:plist文件的新建与读取给UITableView设置变化的值单元格的删除、插入及刷新 plist...
    shiwuoo阅读 2,839评论 0 6
  • 整个四月五月都在慌乱中度过,心绪慌乱、工作慌乱、生活也慌乱,我也不能比这更乱了吧,到此时此刻,我的心情还是乱的,没...
    有一天_陌上花阅读 429评论 4 6
  • 因为公司的网络请求都基于Https、Post进行, 而且做了相关加密, 不自己设置下Charles就不能做相关抓包...
    MonsterNanny阅读 12,907评论 9 48

友情链接更多精彩内容