【转】iOS UITableView的方法解析
原文网址:http://www.cnblogs.com/wfwenchao/articles/3718742.html
- - (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;
- }
原文连接:http://sgm881218.iteye.com/blog/1852325
【转】iOS UITableView的方法解析的更多相关文章
- IOS UITableview代理方法总结
tableview的datasource代理 @required的两个数据源方法 1.返回每个 session 中 cell 的个数 - (NSInteger)tableView:(UITableVi ...
- iOS开发init方法解析
自定义的init方法,都必须调用父类的init方法. 一般情况下为: - (id)init { [super init]; xxx = xxx; } 通常情况下,这种模式可以满 ...
- iOS 详解NSXMLParser方法解析XML数据方法
前一篇文章已经介绍了如何通过URL从网络上获取xml数据.下面介绍如何将获取到的数据进行解析. 下面先看看xml的数据格式吧! <?xml version="1.0" enc ...
- [转载]iOS 10 UserNotifications 框架解析
活久见的重构 - iOS 10 UserNotifications 框架解析 TL;DR iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...
- IOS的XML文件解析,利用了NSData和NSFileHandle
如果需要了解关于文档对象模型和XML的介绍,参看 http://www.cnblogs.com/xinchrome/p/4890723.html 读取XML 上代码: NSFileHandle *fi ...
- IOS UITableView NSIndexPath属性讲解
IOS UITableView NSIndexPath属性讲解 查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和deleg ...
- 四种方法解析JSON数据
(1)使用TouchJSon解析方法:(需导入包:#import "TouchJson/JSON/CJSONDeserializer.h") //使用TouchJson来解析北京的 ...
- iOS学习——JSON数据解析(十一)
在之前的<iOS学习——xml数据解析(九)>介绍了xml数据解析,这一篇简单介绍一下Json数据解析.JSON 即 JavaScript Object Natation,它是一种轻量级的 ...
- iOS UITableView划动删除的实现
标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...
随机推荐
- MATLAB——axis
MATLAB——axis axis中文为“轴”之意,在matlab中用于控制坐标轴的范围和样式(颜色等). axis([XMIN XMAX YMIN YMAX]) 设置当前所绘图像的x轴和y轴的范围. ...
- struts2之请求参数接收
struts2之请求参数接收 1. 采用基本类型接受请求参数(get/post)在Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予给同名的属性.请求路径:http:/ ...
- 《head first java 》读书笔记(三)
Updated 2014/04/03 --P518 Thread需要任务,任务是实现过Runnable的实例.Runnalbe这个接口只有一个方法.run()会是新线程所执行的第一项方法.要把Runn ...
- Unity3D与iOS的交互设计<ViewController 的跳转>
原地址:http://www.aichengxu.com/article/%CF%B5%CD%B3%D3%C5%BB%AF/28797_12.html Unity3D与iOS的交互设计<View ...
- Java Memory Basic
转自: http://www.blogjava.net/justinchen/archive/2009/justinchen/archive/2009/01/08/248738.html GC and ...
- hdoj 2112 HDU Today
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=2112 分析:多了一个地方的条件,用map来映射地点编号,Dijkstra求解即可 //2013-10- ...
- EXPRESS.JS再出发
那个那个MEAN的书,看得七七八八,有了大概,现在就要一样一样的加深记忆啦.. EXPRSS.JS的东东,网上有现成入门书籍: 第一期代码测试: var express = require('expr ...
- [优先队列]HDOJ5360 Hiking
题意:有n个人,每个人有两个参数$l$和$r$ 邀请他们去hiking, 当 当前已经邀请到的人数大于等于$l$,并且小于等于$r$,那么这个人就会去 问最多能邀请到几个人 并输出 依次要邀请的人的 ...
- hdu 1005 java(System.out.println();与System.out.println(“\n”);)
//package Main; import java.util.Scanner; public class Main { static int [][] mat=new int [2][2]; st ...
- [转]C++常见内存错误汇总
在系统开发过程中出现的bug相对而言是比较好解决的,花费在这个上面的调试代价不是很大,但是在系统集成后的bug往往是难以定位的bug(最好方式是打桩,通过打桩可以初步锁定出错的位置,如:进入函数前打印 ...