UITableView表

------------------------

表UITableView

------------------------

获取index path

根据cell具体位置(行数或者区数)获取index path

NSIndexPath *indexpath = [NSIndexPath indexPathForRow :0 inSection:0 ];

根据cell获取index path

NSIndexPath *indexPath = [_tbl indexPathForCell :cell];

- ( NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;

根据-点(相对于表的位置,不是行)获取index path

NSIndexPath *indexpath = [_tbl indexPathForRowAtPoint :point];

- ( NSIndexPath *)indexPathForRowAtPoint:(CGPoint )point;

根据tableView被点那行,获取index path

- ( NSIndexPath *)indexPathForSelectedRow;

获取cell

根据indexpath获取cell

UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexpath];

根据cell上的控件,获取父视图;

------------------------------------------------------------------------------

添加和删除数据

------------------------------------------------------------------------------

//插入某一行

NSIndexPath *indexpath =[NSIndexPath indexPathForRow :arrayMu.count- 1 inSection :区];

NSArray *array = [NSArray arrayWithObject :indexpath];

[ self.tableView insertRowsAtIndexPaths:array withRowAnimation :UITableViewRowAnimationFade];

//删除某一行

NSArray *array = [NSArray arrayWithObject :indexPath];

[ self.tableView deleteRowsAtIndexPaths:array withRowAnimation :UITableViewRowAnimationFade];

------------------------------------------------------------------------------

表的刷新

------------------------------------------------------------------------------

方法一:刷新表

[ _tbl reloadData ];

//方法二:刷新某一个区[有动画]

NSIndexSet *set = [NSIndexSet indexSetWithIndex :ctl.tag- 1];

[ _tbl reloadSections :set withRowAnimation :UITableViewRowAnimationMidAlertViewdle ];

------------------------------------------------------------------------------

输入cell的时候,自动滚到最后一行

------------------------------------------------------------------------------

//到最后一行

if ((_tabView .contentSize. height+106 ) > _tabView .frame. size.height ) {

CGPoint offset = CGPointMake (0, _tabView .contentSize. height);

[ _tabView setContentOffset :offset animated :NO];

}

------------------------------------------------------------------------------

xib中使用

------------------------------------------------------------------------------

1??记得设置xib中的重用标识符

if (cell == nil ) {

//如果cell为空,从xib文件中加载cell

//NSBundle 应用程序束,可以获取工程中所有文件的路径

NSArray *array = [[NSBundle mainBundle ]loadNibNamed: @"ZYTableViewCell" owner :nil options:nil ];

for (id obj  in array) {

if ([obj isKindOfClass : [ZYTableViewCell class]])

{

cell = obj;

break;

}

}

------------------------------------------------------------------------------

------------------------------------------------------------------------------

代理 <UITableViewDataSource,UITableViewDelegate >

创建: //多个表,必须需在初始化设置tyle:UITableViewStyleGrouped

_tableView = [[UITableView alloc] initWithFrame :[[UIScreen mainScreen]bounds ]  style :UITableViewStyleGrouped];

[ self.view addSubview: _tableView];

//设置代理

_tableView .delegate = self;

_tableView .dataSource = self;

//分割线颜色/样式

tbl. separatorColor / tbl.separatorStyle ;

//设置区数//默认1个区

- (NSInteger )numberOfSectionsInTableView:( UITableView *)tableView

//设置行数;

- ( NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger )section

//主要单元格内容内容

- ( UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

重用方法一:

//在viewDidLoad中注册常规的单元格

[ _tbl registerClass :[UITableViewCell class] forCellReuseIdentifier :@"reuesIdentifier"];

//重用队列中取不到cell时,统会帮你生成相应的单元格;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :@"reuesIdentifier"];

1.在自定义类的init中创建控件

重用方法二:

//定义一个重用标示符->从重用队列中取出cell->如果cell为空,创建cell

static NSString *cellReuseIndetifier = @"cellReuseIndetifier" ;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :cellReuseIndetifier];

if (cell == nil ) {

//  UITableViewCellStyleDefault 没有detailTextLabel.text

cell = [[ UITableViewCell alloc ] initWithStyle :UITableViewCellStyleValue1 reuseIdentifier :cellReuseIndetifier];

1.在if之内创建控件,在if之外设置数据

}

cell. textLabel.text = _numQuArray [indexPath.section][indexPath. row];

cell. imageView.image =_numtpArray [indexPath.section][indexPath. row];

cell.selectionStyle  选中样式

//设置右边文字,若要在下面则cell->style:UITableViewCellStyleSubtitle

cell. detailTextLabel.text =_ageNumArray [indexPath.section];

//设置最右边样式,当设置accessoryView的时候accessoryType被忽略;

cell. accessoryType = UITableViewCellAccessoryCheckmark ;

return cell;

}

//单元格被点的时候,会触发这个方法

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

//当附件按钮被点的时候,此方法被调用。可以触发UITableViewCellAccessoryDetailDisclosureButton,UITableViewCellAccessoryDetailButton

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

//设置区头->区尾titleForFooterInSection

- ( NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger )section

//区尾高度->区头高度: heightForHeaderInSection

- ( CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger )section

//设置行高

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

//区尾视图 ->区头视图 viewForHeaderInSection

- ( UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger )section

{

return lab;//如果示自己设置内存则加antorelease

}

------------------------------------------------------------------------------

设置附件按钮的类型

------------------------------------------------------------------------------

-( UITableViewCellAccessoryType )tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {

return UITableViewCellAccessoryDisclosureIndicator ;

}

//设置附件按钮的类型

//cell.accessaryType = ...

//cell.accessaryView = ...

//当附件按钮被点时会被调用

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

}

------------------------------------------------------------------------------

用自定义单元格

------------------------------------------------------------------------------

用系统单元格,给系统单元格添加控件1.添加lbl,2.添加btn(点击事件1.找父视图cell;2.绑定UIEvent事件;3.自定义的button)

用自定义单元格:

1.纯代码

2.xib,设置Identifier

3.xib 的注册写法

------------------------------------------------------------------------------

获取按钮的点击事件

------------------------------------------------------------------------------

/*获取按钮的点击事件

1.获取btn的父视图cell, 根据cell获取indexpath

2.获取触摸点在tableview中的位置,根据这个点获取indexpath

3.用自定义的button

*/

//方法一:

-( void)onbuttonClick:(UIButton *)btn {

//1.获取cell

UIView *view = btn.superview ;

UITableViewCell  *cell = (UITableViewCell *)view.superview ;

/*2.根据cell获取indexPath

- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;

*/

NSIndexPath *indexPath = [_tbl indexPathForCell :cell];

NSLog(@"%d %d",indexPath. section,indexPath.row );

}

//方法二:

- ( void)onCellButtonClick:(id)sender forEvent:(UIEvent *)event {

//获取事件所有的触摸点

NSSet *set = [event allTouches ];

//取出集合中的任意一个触摸点

UITouch *aTouch = [set anyObject];

//获取这个触摸点在tableview中的位置

CGPoint point = [aTouch locationInView: _tbl];

NSLog(@"%@" ,NSStringFromCGPoint(point));

//根据点point在表tableview中的位置,获取索引路径indexPath

NSIndexPath *indexpath = [_tbl indexPathForRowAtPoint :point];

NSLog(@"%d  %d",indexpath. section,indexpath.row );

}

------------------------------------------------------------------------------

表的折合方式:

------------------------------------------------------------------------------

1??行的数量:判断是否;若为否则return 0;

2??表头中建立UIControl添加addTarget事件

3??在区头事件中

方法一:刷新表

[ _tbl reloadData ];

//方法二:刷新某一个区[有动画]

NSIndexSet *set = [NSIndexSet indexSetWithIndex :ctl.tag- 1];

[ _tbl reloadSections :set withRowAnimation :UITableViewRowAnimationMidAlertViewdle ];

具体实例:

定义

BOOL _isExpand[2];

设置

memset (_isExpand, 1, sizeof (_isExpand));

//设置行数

- ( NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger )section

{

if (_isExpand [section]) {

return [_arrayNum [section] count];

}

return 0 ;

}

//设置区头view

- ( UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger )section

{

//折合效果

UIControl *tou = [[UIControl alloc] init];

tou. frame = CGRectMake (0, 0, 160 , 40);

tou. backgroundColor = [UIColor greenColor];

tou. tag = section+1 ;

[tou addTarget:self action :@selector(onTouClick:) forControlEvents :UIControlEventTouchUpInside];

return tou;

}

- ( void)onTouClick:(UIControl *)tou

{

_isExpand[tou.tag -1] =! _isExpand[tou.tag -1];

[ _tableView reloadData ];

}

----------------------------------------------------

编辑

----------------------------------------------------

//出现编辑按钮

self.navigationItem .rightBarButtonItem = self .editButtonItem;

表的编辑形式

self .tableView. editing = !self .tableView. editing;

返回编辑类型

-( UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

5.控制能不能编辑

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

------------------------------------------------------------------------------

//------------表的添加

- (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

return UITableViewCellEditingStyleInsert;

}

//添加

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

{

//数值中第n行插入str

[ _numArray insertObject :str atIndex :indexPath.row];

//刷新方法一

[ self.tableView reloadData];

//刷新方法二

// NSArray *array = [NSArray arrayWithObject :indexPath];

[ self.tableView deleteRowsAtIndexPaths:array withRowAnimation :UITableViewRowAnimationFade];

}

------------------------------------------------------------------------------

//------------删除

1.设置tableview的编辑样式

- ( void)onBarBtnClick

{

self.tableView .editing = ! self.tableView .editing;

}

2. 设置编辑状态

- (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

return UITableViewCellEditingStyleDelete ;

}

3. //设置点击删除触发事件

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

{

[ _nsArrayNum removeObjectAtIndex :indexPath.row];

//刷新方法一

//[self.tableView reloadData];

//刷新方法二

NSArray *array = [NSArray arrayWithObject :indexPath];

[ self.tableView deleteRowsAtIndexPaths:array withRowAnimation :UITableViewRowAnimationFade];

}

4. //设置删除按钮的标题

- ( NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

return @"删" ;

}

------------------------------------------------------------------------------

//------------移动

1.设置表的编辑状态yes

- ( void)onBarClcik

{

self.tableView .editing = ! self.tableView .editing;

}

2.

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

{

//获取原来的区

NSMutableArray *arrayYiDong = _arrayMuLei [sourceIndexPath. section];

//获取原来的数据

id obj = [arrayYiDong objectAtIndex :sourceIndexPath. row];

//删除原来数据

[arrayYiDong removeObjectAtIndex :sourceIndexPath. row];

//获取要改变的区

NSMutableArray *arrayYiDongStop = _arrayMuLei [destinationIndexPath. section];

//改变数据

[arrayYiDongStop insertObject:obj atIndex :destinationIndexPath. row];

}

3.

设置能否移动,默认yes

- ( BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

return UITableViewCellEditingStyleInsert ;

}

时间: 2024-08-03 15:23:24

UITableView表的相关文章

UI第九讲.UITableView表视图创建,表视图的重用机制,表视图的相关配置方法

一.UITableView表视图创建 1>.基本属性: UITableView继承自UIScrollView,所以可以滚动          表视图的每一条数据都是显示在UITableViewCell对象中          表视图可以分区显示数据,每个分区称为一个section,每一行称为row,编号都是从0始 2>.重要用法: 最重要的是两个代理方法 <UITableViewDelegate,UITableViewDataSource>(其中必须实现的是 numberOfRow

UITableView, 表视图

UITableView, 表视图     样式     1.UITableViewStylePlain, 正常样式     2.UITableViewStyleGrouped,  分组样式 行高, 默认44 tableView.rowHeight = 80; 分隔线的颜色 tableView.separatorColor = [UIColor orangeColor]; tableView.separatorStyle = UITableViewCellSeparatorStyleSingleL

UITableView表视图

待完善 UITableView表视图,布布扣,bubuko.com

UI学习笔记---第十天UITableView表视图编辑

UITableView表视图编辑 表视图编辑的使用场景 当我们需要手动添加或者删除某条数据到tableView中的时候,就可以使用tableView编辑.比如微信 扣扣中删除和某人的通话 当我们需要手动调整单元格的顺序时,就可以通过tableView移动,移动单元格到指定位置 代理AppDelegate.m中代码 #import "AppDelegate.h" #import "RootViewController.h" @implementation AppDel

UI学习笔记---第十一天UITableView表视图高级-自定义cell

自定义cell,多类型cell混合使用,cell自适应高度 自定义cell就是创建一个UITableViewCell的子类 把cell上的空间创建都封装在子类中,简化viewController中的代码 cell中的空间如何显示Model中的信息 cell中声明一个Model类型的属性,viewController中获取到Model对象后赋值给cell的Model属性 cell中重写Model的setter方法,把Model对象中的内容重新赋值给各个控件 M和V不直接通信,C负责M和V之间进行通

UITableView表视图以及重建机制

表视图UITableView 表视图UITableView,是IOS中最重要的视图,随处可见 表视图通常用来管理一组具有相同数据结构的数据 UITableView继承自UIScrollView,所以可以滚动 表视图的每一条数据都是显示在UITableViewCell对象中 表视图可以分区显示数据,每个分区称为一个section,每一行称为row,编号都是从0开始 tableView的样式是个枚举类型,有两种样式:plain和grouped可以根据在不同的使用场景下设置不同的样式 typedef

UI基础:UITableView表视图

表视图 UITableView,iOS中最重要的视图,随处可见. 表视图通常用来管理一组具有相同数据结构的数据. UITableView继承于UIScrollView,所以可以滚动 表视图的每条数据都是显示在UITableViewCell对象中 表视图可以分区显示数据,每个分区称为一个section,每一行称为 row,编号都是从0开始 表视图的创建 DataSource数据源 我们需要给tableView指定一个数据源,它负责给tableView提供数据. 需要实现协议中两个必须实现的方法.

UI开发----UITableView表视图-1

//  Created By 郭仔   2015年04月22日22:12:47 // ================================== 时间都去哪了!!!!!!! // ================================== 表视图 UITableView,iOS中最重要的视图,随处可?见. 表视图通常?用来管理?一组具有相同数据结构的数据. UITableView继承?自UIScrollView,所以可以滚动 表视图的每?一条数据都是显?示在UITableVi

UI_09 UITableView(表视图)

?.表视图 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView.UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳 1.表示图的样式 UITableViewStylePlain UITableViewStyleGrouped 2.表示图创建 步骤: 创建 UITableView *tableView= [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewS