UITabView使用详解
在开发iphone的应用时基本上都要用到UITableView,这里讲解一下UITableView的使用方法及代理的调用情况
- (void)viewDidLoad { [super viewDidLoad]; //初始化数据 NSArray *array1_=@[@"张铁林",@"张国立",@"张国荣",@"张艺谋",@"张惠妹"]; NSArray *array2_=@[@"李小龙",@"李小路"]; NSArray *array3_=@[@"王刚"]; self.myDic=@{@"老张家":array1_, @"老李家":array2_, @"老王家":array3_}; UITableView *myTableView_=[[UITableView alloc] initWithFrame:CGRectMake(, ,, ) style:UITableViewStylePlain]; myTableView_.delegate=self; myTableView_.dataSource=self; //改变换行线颜色lyttzx.com myTableView_.separatorColor = [UIColor blueColor]; //设定Header的高度, myTableView_.sectionHeaderHeight=; //设定footer的高度, myTableView_.sectionFooterHeight=; //设定行高 myTableView_.rowHeight=; //设定cell分行线的样式,默认为UITableViewCellSeparatorStyleSingleLine [myTableView_ setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine]; //设定cell分行线颜色 [myTableView_ setSeparatorColor:[UIColor redColor]]; //编辑tableView myTableView_.editing=NO; [self.view addSubview:myTableView_]; //跳到指的row or section [myTableView_ scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:2inSection:] atScrollPosition:UITableViewScrollPositionBottom animated:NO]; } //指定有多少个分区(Section),默认为1 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [[self.myDic allKeys] count]; } //每个section底部标题高度(实现这个代理方法后前面 sectionHeaderHeight 设定的高度无效) -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return ; } //每个section头部标题高度(实现这个代理方法后前面 sectionFooterHeight 设定的高度无效) -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return ; } //每个section头部的标题-Header - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [[self.myDic allKeys] objectAtIndex:section]; } //每个section底部的标题-Footer - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ return nil; } //用以定制自定义的section头部视图-Header -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ return nil; } //用以定制自定义的section底部视图-Footer -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ UIImageView *imageView_=[[UIImageView alloc]initWithFrame:CGRectMake(, ,, )]; imageView_.image=[UIImage imageNamed:@"1000.png"]; return [imageView_ autorelease]; } //指定每个分区中有多少行,默认为1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [[self.myDic objectForKey:[[self.myDic allKeys]objectAtIndex:section]]count]; } //改变行的高度(实现主个代理方法后 rowHeight 设定的高度无效) - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return ; } //绘制Cell -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: SimpleTableIdentifier] autorelease]; //设定附加视图 [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; // UITableViewCellAccessoryNone, // 没有标示 // UITableViewCellAccessoryDisclosureIndicator, // 下一层标示 // UITableViewCellAccessoryDetailDisclosureButton, // 详情button // UITableViewCellAccessoryCheckmark // 勾选标记 //设定选中cell时的cell的背影颜色 cell.selectionStyle=UITableViewCellSelectionStyleBlue; //选中时蓝色效果 // cell.selectionStyle=UITableViewCellSelectionStyleNone; //选中时没有颜色效果 // cell.selectionStyle=UITableViewCellSelectionStyleGray; //选中时灰色效果 // // //自定义选中cell时的背景颜色 // UIView *selectedView = [[UIView alloc] initWithFrame:cell.contentView.frame]; // selectedView.backgroundColor = [UIColor orangeColor]; // cell.selectedBackgroundView = selectedView; // [selectedView release]; // cell.selectionStyle=UITableViewCellAccessoryNone; //行不能被选中 } //这是设置没选中之前的背景颜色 cell.contentView.backgroundColor = [UIColor clearColor]; cell.imageView.image=[UIImage imageNamed:@"1001.jpg"];//未选cell时的图片 cell.imageView.highlightedImage=[UIImage imageNamed:@"1002.jpg"];//选中cell后的图片 cell.textLabel.text=[[self.myDic objectForKey:[[self.myDicallKeys]objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row]; return cell; } //行缩进 -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger row = [indexPath row]; return row; } //选中Cell响应事件 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失 //得到当前选中的cell UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath]; NSLog(@"cell=%@",cell); }
//已经选中行(常用)
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//作用:跳转下一个页面,并且把当前选中的单元格数据,传递过去
//寻找选中的单元格
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// //通过cell获取indexPath
// NSIndexPath *path = [tableView indexPathForCell:cell];
QYDetailInfoViewController *detailInfoVC = [[QYDetailInfoViewController alloc] init];
detailInfoVC.titleString = cell.textLabel.text;
[self.navigationController pushViewController:detailInfoVC animated:YES];
}
//行将显示的时候调用,预加载行 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"将要显示的行是\n cell=%@ \n indexpath=%@",cell,indexPath); } //选中之前执行,判断选中的行(阻止选中第一行) -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; if (row == ) return nil; return indexPath; } //编辑状态,点击删除时调用 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { } //cell右边按钮格式为UITableViewCellAccessoryDetailDisclosureButton时,点击按扭时调用的方法 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{ NSLog(@"当前点击的详情button \n indexpath=%@",indexPath); } //右侧添加一个索引表 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return [self.myDic allKeys]; } //划动cell是否出现del按钮 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath { return YES; } //设定横向滑动时是否出现删除按扭,(阻止第一行出现) -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row==) { return UITableViewCellEditingStyleNone; } else{ return UITableViewCellEditingStyleDelete; } return UITableViewCellEditingStyleDelete; } //自定义划动时delete按钮内容 - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ return @"删除这行"; } //开始移动row时执行 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath { NSLog(@"sourceIndexPath=%@",sourceIndexPath); NSLog(@"sourceIndexPath=%@",destinationIndexPath); } //滑动可以编辑时执行 -(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"willBeginEditingRowAtIndexPath"); } //将取消选中时执行, 也就是上次先中的行 -(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"上次选中的行是 \n indexpath=%@",indexPath); return indexPath; } //让行可以移动 -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath { return NO; } //移动row时执行 -(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath*)proposedDestinationIndexPath { NSLog(@"targetIndexPathForMoveFromRowAtIndexPath"); //用于限制只在当前section下面才可以移动 if(sourceIndexPath.section != proposedDestinationIndexPath.section){ return sourceIndexPath; } return proposedDestinationIndexPath; }
UITabView使用详解的更多相关文章
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
- Java 字符串格式化详解
Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- Git初探--笔记整理和Git命令详解
几个重要的概念 首先先明确几个概念: WorkPlace : 工作区 Index: 暂存区 Repository: 本地仓库/版本库 Remote: 远程仓库 当在Remote(如Github)上面c ...
- Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)
Android XML shape 标签使用详解 一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...
- Node.js npm 详解
一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...
随机推荐
- 【题解】Luogu P2153 [SDOI2009]晨跑
原题传送门 一眼应该就能看出是费用流 因为每个交叉路口只能通过一次,所以我们进行拆点,连一条流量为1费用为0的边 再按照题目给的边(是单向边)建图 跑一下MCMF就行了 拆点很套路的~ #includ ...
- freeswitch 修改系统最大呼叫量
freeswitch 中有2个参数限制系统的最大呼叫量,以防止资源耗尽. max_session控制最大并发数.默认值1000:sps控制最大每秒呼叫量,默认值30 命令临时生效:fsctl max_ ...
- Lintcode175-Revert Binary Tree-Easy
175. Invert Binary Tree Invert a binary tree. Example Example 1: Input: {1,3,#} Output: {1,#,3} Expl ...
- abstract class和interface的异同
含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不必 ...
- ssm 整合(方案二 maven)
通过maven来整合ssm方便很多,至少不用去找jar包 具体架构如下: 1.配置pom.xml <project xmlns="http://maven.apache.org/POM ...
- Robot Framework问题记录
robotframework运行时后台报错UnicodeDecodeError UnicodeDecodeError :'utf-8' codec can't decode byte 0xb2 in ...
- Jellyfish详解
一.Jellyfish简介 JELLYFISH是CBCB(Center for Bioinformatics and Computational Biology)的Guillaume Marçais ...
- Python自学:第三章 索引从0开始而不是从1
#返回最后一个,和倒数第二个元素 bicycles = ['trek','cannondale','redline','specialized'] print(bicycles[-1]) print( ...
- mpvue构建小程序(步骤+地址)
mpvue 是一个使用 Vue.js 开发小程序的前端框架(美团的开源项目).框架基于 Vue.js 核心,mpvue 修改了 Vue.js 的 runtime 和 compiler 实现,使其可以运 ...
- 『Python』装饰器
一.参考 作者:zhijun liu 链接:https://www.zhihu.com/question/26930016/answer/99243411 来源:知乎 建议大家去原答案浏览 二.装饰器 ...