(转载)UITableView使用详解

在开发iphone的应用时基本上都要用到UITableView,这里讲解一下UITableView的使用方法及代理的调用情况
UITableView使用详解

- (void)viewDidLoad
{
    [super viewDidLoad];
    //初始化数据
    NSArray *[email protected][@"张铁林",@"张国立",@"张国荣",@"张艺谋",@"张惠妹"];
    NSArray *[email protected][@"李小龙",@"李小路"];
    NSArray *[email protected][@"王刚"];
    self.myDic[email protected]{@"老张家":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://blog.sina.com.cn/s/blog_9693f61a01016lv5.html

时间: 2024-12-12 21:10:41

(转载)UITableView使用详解的相关文章

[转载]WebConfig配置文件详解

<?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual Studio 中的“网站”->“Asp.Net 配置”选项. 设置和注释的完整列表在 machine.config.comments 中,该文件通常位于 "Windows"Microsoft.Net"Framework"v2.x"Config 中.--&g

IOS中表视图(UITableView)使用详解

IOS中UITableView使用总结 一.初始化方法 - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style; 这个方法初始化表视图的frame大小并且设置一个风格,UITableViewStyle是一个枚举,如下: ? 1 2 3 4 typedef  NS_ENUM(NSInteger, UITableViewStyle) {      UITableViewStylePlain,         

[转载] 多图详解Spring框架的设计理念与设计模式

转载自http://developer.51cto.com/art/201006/205212_all.htm Spring作为现在最优秀的框架之一,已被广泛的使用,51CTO也曾经针对Spring框架中的JDBC应用做过报道.本文将从另外一个视角试图剖析出Spring框架的作者设计Spring框架的骨骼架构的设计理念. AD: Spring作为现在最优秀的框架之一,已被广泛的使用,51CTO也曾经针对Spring框架中的JDBC应用做过报道.本文将从另外一个视角试图剖析出Spring框架的作者

(转载)maven详解

转载: http://www.cnblogs.com/hongwz/p/5456578.html Maven详解 一.前言     以前做过的项目中,没有真正的使用过Maven,只知道其名声很大,其作用是用来管理jar 包的.最近一段时间在项目过程中使用Maven,用Maven构建的web项目,其项目结构只停留在了解阶段,没有深入的使用与理解,刚好最近看了一篇关于Maven的详解:就开始深入学习一下Maven的具体应用. 二.Maven的作用 在开发中,为了保证编译通过,我们会到处去寻找jar包

【转载】log4j详解使用

log4j详解 日志论    在应用程序中输出日志有有三个目的:(1)监视代码中变量的变化情况,把数据周期性地记录到文件中供其他应用进行统计分析工作. (2)跟踪代码运行进轨迹,作为日后审计的依据. (3)担当集成开发环境中的调试器,向文件或控制台打印代码的调试信息.  Apache能用日志包(Commons Logging Package)是Apache的一个开放源代码项目,它提供了一组通用的日志接口,用户可以自由地选择实现日志接口的第三方软件.通用日志包目前支持以下日志实现: Log4J日志

[转载]ssget 用法详解 by yxp

总结得很好的ssget用法.....如此好文,必须转载. 原文地址: http://blog.csdn.net/yxp_xa/article/details/72229202 ssget 用法详解 by yxp 2017/04/10 ALisp 编程时 ssget 是无处不在的,灵活的选择集构建方式,可以轻松的处理图形,体现了 Lisp 相对其他语言的优点. 然而强大的 ssget 估计很多人都不清楚用法,包括各种教程对 ssget 的描述都不够完善,本文试图总结 ssget 的用法,不足之处请

(转载)HTTP协议详解(真的很经典)

转自:http://blog.csdn.net/gueter/archive/2007/03/08/1524447.aspx Author :Jeffrey 引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1.0的第六版,HTTP/1.1的规范化工作正在进行之中,而且HTTP-NG(Next Generation of HTTP)的建议已经提出.HT

(转载)log4net 组件详解

1.概述 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.本文主要是介绍如何在Visual Studio2008中使用log4net快速创建系统日志,如何扩展以输出自定义字段. 2.一个简单的使用实例 第一步:在项目中添加对log4net.dll的引用,这里引用版本是1.2.10.0. 第二步:程序启动时读取log4net的配置文件. 如果是CS程序,在根目录的Program.cs中的Mai

[转载] HTTP协议详解

转自:http://blog.csdn.net/gueter/archive/2007/03/08/1524447.aspx Author :Jeffrey 引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1.0的第六版,HTTP/1.1的规范化工作正在进行之中,而且HTTP-NG(Next Generation of HTTP)的建议已经提出.HT