一、UITableView的属性

NSIndexPath类型是用来获取用户选择的indexPath,在别的函数里面,若需要知道用户选择了哪个cell,用上它可以省事很多。不必再去建全局变量section和row。

NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow];

1.    UITableView的初始化

UITableView tableview= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];

[tableview setDelegate:self];

[tableview setDataSource:self];

[self.view addSubview: tableview];

[tableview release];

(1) 在初始化UITableView的时候必须实现UITableView的是,在.h文件中要继承UITableViewDelegate和 UITableViewDataSource,并实现3个UITableView数据源方法和设置它的delegate为self,这个是在不直接继承 UITableViewController实现的方法。

(2) 直接在XCODE生成项目的时候继承UITableViewController的,它会帮你自动写好UITableView必须要实现的方法。

(3)UITableView继承自UIScrollView。

2.    UITableView的数据源

(1)UITableView是依赖外部资源为新表格单元填上内容的,我们称为数据源,这个数据源可以根据索引路径提供表格单元格,在UITableView中,索引路径是NSIndexPath的对象,可以选择分段或者分行,即是我们编码中的section和row。

(2)UITableView有三个必须实现的核心方法,分别如下:

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;

这个方法可以分段显示或者单个列表显示我们的数据。

-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section;

这个方法返回每个分段的行数,不同分段返回不同的行数可以用switch来做,如果是单个列表就直接返回单个你想要的函数即可。

-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath;

这个方法是返回我们调用的每一个单元格。通过我们索引的路径的section和row来确定。

3.    UITableView的委托方法

使用委托是为了响应用户的交互动作,比如下拉更新数据和选择某一行单元格,在UITableView中有很大这种方法供我们选择。

(1) 委托方法讲解

//添加索引

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

return TitleData;

}

//设置每个section显示的Title

- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{

return @"Andy-清风";

}

//指定有多少个分区(Section),默认为1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 2;

}

//指定每个分区中有多少行,默认为1

- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section{

}

//设置每行调用的cell

-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:

SimpleTableIdentifier];

if (cell == nil) {

cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:SimpleTableIdentifier] autorelease];

}

cell.imageView.image=image;//未选cell时的图片

cell.imageView.highlightedImage=highlightImage;//选中cell后的图片

cell.text=@”Andy-清风”;

return cell;

}

//设置让UITableView行缩进

-(NSInteger)tableView:(UITableView *)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{

NSUInteger row = [indexPath row];

return row;

}

//设置cell每行间隔的高度

- (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 40;

}

//返回当前所选cell

NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];

[TopicsTable selectRowAtIndexPath:ip animated:YESscrollPosition:UITableViewScrollPositionNone];

//设置UITableView的style

[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];

//设置选中Cell的响应事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失

}

//设置选中的行所执行的动作

-(NSIndexPath *)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

NSUInteger row = [indexPath row];

return indexPath;

}

//设置划动cell是否出现del按钮,可供删除数据里进行处理

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {

}

//设置删除时编辑状态

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

{

//右侧添加一个索引表

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

}

(2)  其他

//选中cell时的颜色,在官方文档有如下可以选择

typedef enum {

UITableViewCellSelectionStyleNone,

UITableViewCellSelectionStyleBlue,

UITableViewCellSelectionStyleGray

} UITableViewCellSelectionStyle

//cell右边按钮格式

typedef enum {

UITableViewCellAccessoryNone,                   //don't show any accessory view

UITableViewCellAccessoryDisclosureIndicator,    //regular chevron. doesn't track

UITableViewCellAccessoryDetailDisclosureButton, //blue button w/ chevron. tracks

UITableViewCellAccessoryCheckmark               //checkmark. doesn't track

} UITableViewCellAccessoryType

//是否加换行线

typedef enum {

UITableViewCellSeparatorStyleNone,

UITableViewCellSeparatorStyleSingleLine

} UITableViewCellSeparatorStyle

//改变换行线颜色

tableView.separatorColor= [UIColor blueColor];

二、UITableView的代理方法

#pragma mark 每一行的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

#pragma mark 选中了某一行就会调用

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

#pragma mark 取消选中了某一行就会调用

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

#pragma mark 当用户提交了一个编辑操作就会调用(比如点击了“删除”按钮)

// 只要实现了这个方法,就会默认添加滑动删除功能

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

#pragma mark 当移动了某一行cell就会调用

// 只要实现了这个方法,就会默认添加排序功能

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

二、修改Cell的状态

1.最好通过“修改模型数据”来修改Cell的状态

2.修改步骤

1> 修改模型数据

2> 刷新表格

* 整体刷新:reloadData(最重要)

* 局部刷新:reloadRowsAtIndexPaths:withRowAnimation:

三、UITableView常见方法

1.取消选中某一行(去掉cell选中时默认的蓝色背景)

- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

2.局部刷新(仅仅刷新indexPaths数组中装着的行)

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

3.整体刷新(屏幕中的每一行都刷新)

- (void)reloadData;

4.直接删除界面上的行数(要求模型数据也要删掉对应的数量)

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

5.设置编辑模式

@property(nonatomic,getter=isEditing) BOOL editing;

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

// 注意:

不管是局部刷新,还是整体刷新,原理都是:

UITableView重新向数据源(dataSource)和代理(delegate)发送相应的消息,最终将得到的数据展示出来

一、UITableView的属性的更多相关文章

  1. IOS开发UI基础UITableView的属性

    UITableView UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped <UITableViewDataSour ...

  2. IOS UITableView NSIndexPath属性讲解

    IOS UITableView NSIndexPath属性讲解   查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和deleg ...

  3. UITableView常用属性和方法 - 永不退缩的小白菜

    UITableView常用属性和方法 - 永不退缩的小白菜 时间 2014-05-27 01:21:00  博客园精华区原文  http://www.cnblogs.com/zhaofucheng11 ...

  4. 给iOS开发新手送点福利,简述UITableView的属性和用法

    UITableView UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped   <UITableViewDataSo ...

  5. UIScrollView,UICollectionView 和UITableView的属性和方法

    UIScrollView,UICollectionView 和UITableView 三者之间的关系:UIScrollView是 UICollectionView 和 UITableView 的父类. ...

  6. iOS:UITableView 方法 属性

    参考:https://developer.apple.com/library/iOS/documentation/UIKit/Reference/UITableView_Class/Reference ...

  7. 12-27 UITableView常用属性及方法

    UITableView也有自己的代理协议,它本身继承自UIScrollView 一:代理要遵守代理协议<UITableViewDelegate>,代理协议中的代理方法: 1.改变某一行的行 ...

  8. 全局设置UITableView的属性|正确计算contentSize|MJRefresh mj_footer 能正常隐藏在底部,不因为数据过少展示在页面中部

    可在AppDelegate中设置 if (@available(iOS 11.0, *)) { UITableView.appearance.estimatedRowHeight = 0; UITab ...

  9. ios基础篇(十四)——UITableView(二)属性及基本用法

    上一篇说了UITableView的重用机制,让我们对UITableView有了简单了解,下面说说UITableView的属性及常见方法. 一.属性 1.frame:设置控件的尺寸和大小 2.backg ...

随机推荐

  1. angularJS 服务二

    $http服务 一 介绍 AngularJS为我们提供了很多种服务,$http用于发送http请求,动态的请求数据.我们可以使用内置的$http服务直接同外部进行通信.$http服务只是简单的封装了浏 ...

  2. 【.NET调用Python脚本】C#调用python requests类库报错 'module' object has no attribute '_getframe' - IronPython 2.7

    最近在开发微信公众号,有一个自定义消息回复的需求 比如用户:麻烦帮我查询一下北京的天气? 系统回复:北京天气,晴,-℃... 这时候需要根据关键字[北京][天气],分词匹配需要执行的操作,然后去调用天 ...

  3. Java读书笔记二(封装类)

    1.介绍 都知道java中基本数据类型有非常多,比方string,int--,可是基本数据类型与对象之间是不同的.但非常多情况下,我们希望将基本数据类型当作对象使用,这时候就须要用到封装类. 2.封装 ...

  4. TCP/IP协议原理与应用笔记06:OSI参考模型全称

    1. ISO是一个组织,OSI是一个模型. 2. OSI不是协议,是网络体系结构的概念模型 3. OSI参考模型全称为: Open System Interconnection Reference M ...

  5. Android(java)学习笔记235:多媒体之计算机图形表示方式

    1.多媒体 很多媒体:文字(TextView,简单不讲),图片,声音,视频等等.   2.图片 计算机如何表示图片的? (1)bmp 高质量保存    256色位图:图片中的每个像素点可以有256种颜 ...

  6. python 之路,Day27 - 主机管理+堡垒机系统开发

    python 之路,Day27 - 主机管理+堡垒机系统开发   本节内容 需求讨论 构架设计 表结构设计 程序开发 1.需求讨论 实现对用户的权限管理,能访问哪些机器,在被访问的机器上有哪些权限 实 ...

  7. codevs1002搭桥(建图+Prim)

    /* 先来个灌水法 然后建图跑最小生成树 注意观察题目中的规则 a[1][i]!=a[1][j]&&abs(a[2][i]-a[2][j])<=1 建图的时候可以每一个建筑物都看 ...

  8. 修改Calendar(梅花雨)日历控件 兼容IE9 谷歌 火狐

    修改Calendar日历控件 兼容IE9,谷歌,火狐. 只是能用,出现的位置有所不同,希望有高手再帮我改改吧,谢谢   一. [javascript]   this.iframe     = wind ...

  9. canvas sprite动画 简单封装

    function SpritCtx(img, size, pos, turnTime, totalCount, ctx) { size = size || {}; pos = pos || {}; / ...

  10. MiniProfiler.EF6监控调试MVC5和EF6的性能

    转自:蓝狐学MVC教程 以前开发Webform的时候可以开启trace来跟踪页面事件,这对于诊断程序的性能是有很大的帮助的,起到事半功倍的作用,今天我就来谈用mvc开 发项目的调试和性能监控.EF框架 ...