tableview 详解I
在开发iphone的应用时基本上都要用到UITableView,这里讲解一下UITableView的使用方法及代理的调用情况
UITableView使用详解
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化数据
NSArray *array1_=@[@"张铁林",@"张国立",@"张国荣",@"张艺谋",@"张惠妹"];
NSArray *array2_=@[@"李小龙",@"李小路"];
NSArray *array3_=@[@"王刚"];
self.myDic=@{@"老张家":array1_, @"老李家":array2_, @"老王家":array3_};
UITableView *myTableView_=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
myTableView_.delegate=self;
myTableView_.dataSource=self;
//改变换行线颜色lyttzx.com
myTableView_.separatorColor = [UIColor blueColor];
//设定Header的高度,
myTableView_.sectionHeaderHeight=50;
//设定footer的高度,
myTableView_.sectionFooterHeight=100;
//设定行高
myTableView_.rowHeight=100;
//设定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:2 inSection:2]
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 20;
}
//每个section头部标题高度(实现这个代理方法后前面 sectionFooterHeight 设定的高度无效)
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 20;
}
//每个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(0, 0, 320, 20)];
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 100;
}
//绘制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.myDic allKeys]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 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 == 0)
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==0) {
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;
}
tableview 详解I的更多相关文章
- IOS 中列表的TableView 详解,常用方法整理
一.创建一个列表,不管代码还是nib拖拉,在nib创建的时候,记得加他的二个代理 (UITableViewDelegate UITableViewDataSource) 代码创建的话,需要关联他的代理 ...
- [转载]TableView详解
一.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; [Data ...
- IOS详解TableView——选项抽屉(天猫商品列表)
在之前的有篇文章讲述了利用HeaderView来写类似QQ好友列表的表视图. 这里写的天猫抽屉其实也可以用该方法实现,具体到细节每个人也有所不同.这里采用的是点击cell对cell进行运动处理以展开“ ...
- JavaFx TableView疑难详解
TableView是个十分有用的控件,适应性和灵活性非常强,可以对它进行任意的修改,比如界面样式.功能.本文将从一步步提问的方式讲解TableView 创建已知列的TableView 已知列的表格的创 ...
- TableView 常用技巧与功能详解
分割线顶格iOS8 UITableview分割线顶格的做法 //iOS8 Cell分割线顶格 if ([_tableView respondsToSelector:@selector(setSepar ...
- iPhone应用开发 UITableView学习点滴详解
iPhone应用开发 UITableView学习点滴详解是本文要介绍的内容,内容不多,主要是以代码实现UITableView的学习点滴,我们来看内容. -.建立 UITableView DataTab ...
- Cocos2d-x 3.X手游开发实例详解
Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰 ...
- XML解析之SAX详解
XML解析之SAX详解 本文属于作者原创 http://www.cnblogs.com/ldnh/ XML解析的五个步骤 1.打开文档 (void)parserDidStartDocument:(NS ...
- UITableView详解(1)
一,UITableView控件使用必备,红色部分是易错点和难点 首先创建一个项目,要使用UITableView就需要实现协议<UITableViewDataSource>,数据源协议主要完 ...
随机推荐
- RF环境安装-mac-osx10.10-基础环境-安装指南
一.适用环境: mac系列,osx10.10,自带Python 二.简要步骤: 1. 安装pip,mac自带Python环境,所以我们改成安装pip 2. 安装wxPython,此处我下载的版本是wx ...
- 来看看CBIS 2017中国(上海)大数据产业创新峰会有哪些大师出席
CBIS 2017中国(上海)大数据产业创新峰会,围绕"数据+产业.企业+数据.技术+人才.品牌+市场.应用+发展"相继展开话题讨论. CBIS 2017中国(上海)大数据产业创新 ...
- sqlserver 操作xml
1.xml.exist 输入为XQuery表达式,返回0,1或是Null.0表示不存在,1表示存在,Null表示输入为空 2.xml.value 输入为XQuery表达式,返回一个SQL ...
- sharepoint:各种阀值
//来源:http://www.cnblogs.com/jindahao/archive/2012/04/25/2469791.html 引用自JonyZhu,如下: 技术参数 值 列表最大记录数 5 ...
- centos php 扩展安装
1. 安装mysqli扩展 1.进入php源代码目录:# cd /home/apps/web/php/php-5.3.5/ 2.再进入要添加的mysqli扩展源码目录:# cd ext/mysqli/ ...
- datagrid、easyui-dialog
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 移动端audio自动播放问题
中秋临近,心血来潮想做个手机端贺卡,以前接触的移动端较少,虽然是个简单的贺卡,其实也蛮多坑的,简略说一下在制作贺卡的过程遇到的坑: 一:移动端的屏幕大小不能算作body的大小,因为手机浏览器头部都有网 ...
- Linux Kernel Vhost 架构
Vhost 回顾 Linux中的vhost驱动程序提供了内核virtio设备仿真. 通常,QEMU用户空间进程模拟guest的I / O访问. Vhost将virtio仿真代码放到内核中,使QEMU用 ...
- 转:IIS虚拟目录实现与文件服务器网络驱动器映射共享
这篇文章转载别人,想原创作者致敬! 我本人也遇到同样的问题,故转载记录. 本文重点描述如何使用IIS访问共享资源来架设站点或执行 ASP.Net 等脚本. 通常情况下,拥有多台服务器的朋友在使用IIS ...
- MPlayerX播放视频屏幕中间有图标遮挡的解决办法
问题如下: 解决办法: 在应用程序文件夹中找到MPlayerX,鼠标右击应用图标,在右键菜单中选择"显示包内容",进入如下目录:Contents->Resources,找到l ...