UITableView 常用方法(转自CSDN)

一、概述 
UITableView是iOS开发比不可少也是最重要的一个控件类。可以说任何一个做iOS开发的人都必须熟练使用和掌握它。本文主要就是提供一个学习使用TableView的指南。 
要说UITableView必须要介绍他的几个亲戚:UITableViewDelegate,UITableViewDataSource,UITableViewCell。其中前两个是TableView遵守的两个protocol(别告诉我你不知道啥叫protocol哦)。然后本文会再列出TableView最常用最重要的一些知识点。最后再介绍几个参考例子。 

二、UITableView和它的亲戚们 

1.
UITableView 
参考: 
https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html

1)
初始化 UITableView对象 
– initWithFrame:style:  //
代码生成方式,如果你在nib里加的tableview不需要使用这个方法 
2)配置TableView 

dequeueReusableCellWithIdentifier: //
必须要实现的方法,与TableView同生同死 
  style  property // 有两种
UITableViewStylePlain, UITableViewStyleGrouped,经常用 

numberOfRowsInSection:  //一个section有多少行,经常用 

numberOfSections  //一个TableView有多少个section,经常用 
 
rowHeight  property //
行高,和tableView:heightForRowAtIndexPath:有性能上的区别 
 
separatorStyle  property // cell之间的分割线?待确认 
 
separatorColor  property // 同上 
 
backgroundView  property // tableview的背景view, 这个背景view在所有cell, header
views, footer views之后 
  tableHeaderView 
property // tableview上方的一个headerView, 和delete里的section
header不是一个概念 
  tableFooterView  property //
tableview下方的一个footerview 
  sectionHeaderHeight 
property // section Header的高度, 
 
sectionFooterHeight  property // sectjion
Footer的高度 
  sectionIndexMinimumDisplayRowCount 
property //  功能待确认? 参考例子:  TheElements 
3)
访问Cells和Sections 
– cellForRowAtIndexPath:
//根据IndexPath返回cell 
– indexPathForCell:
//根据cell返回它的indexPath,和上面的方法互补 

indexPathForRowAtPoint://根据一个几何点返回indexPath,如果超过边界返回nil 

indexPathsForRowsInRect:
//根据一个几何的矩形返回矩形所覆盖的行,返回是一个indexPath数组 
– visibleCells //
不清楚怎么用,待确认 
– indexPathsForVisibleRows
//同上 
4) 滚动TableView 

scrollToRowAtIndexPath:atScrollPosition:animated: //
滚动到指定位置 

scrollToNearestSelectedRowAtScrollPosition:animated: //
同上 
5) 管理sections 

indexPathForSelectedRow //返回选定行的indexPath,单行 

indexPathsForSelectedRows //返回选定行的indexPath数组,多行 

selectRowAtIndexPath:animated:scrollPosition:
//根据indexPath选择一行 
– deselectRowAtIndexPath:animated:
//反选一行,有何用? 
  allowsSelection  property
//是否允许用户选取一行 
  allowsMultipleSelection  property
// 是否选取多行,缺省为NO. 可以试试YES后的效果,哈哈 
 
allowsSelectionDuringEditing  property //
编辑模式时是否可选取一行 
 
allowsMultipleSelectionDuringEditing  property //
编辑模式时可否选取多行 
6) 插入、删除、移动行和sections 

beginUpdates // 和endUpdates一起用,让插入、删除、选择操作同时动画,没用过 

endUpdates // 
– insertRowsAtIndexPaths:withRowAnimation:
//根据indexPath数组插入行 

deleteRowsAtIndexPaths:withRowAnimation:
//根据indexPath数组删除行 
– moveRowAtIndexPath:toIndexPath:
//移动一行到另一行 
– insertSections:withRowAnimation:
//插入sections 
– deleteSections:withRowAnimation:
//删除sections 
– moveSection:toSection:
//移动section 
7) 管理和编辑cell 
 
editing  property // YES进入编辑模式,tableview
cell会出现插入、删除、重排序的控件 
– setEditing:animated:
//设置进入退出编辑模式 
8) 重新加载TableView 

reloadData // 重建整个表,包括cells、header、footer,indexs 

reloadRowsAtIndexPaths:withRowAnimation: //
改进,不用reload整个表 
– reloadSections:withRowAnimation: //
同上 
– reloadSectionIndexTitles //
同上 
9) 访问TableView的画图区 

rectForSection: // 返回指定section的矩形 
– rectForRowAtIndexPath:
//返回indexPath指定行的矩形 
– rectForFooterInSection: //
返回section的footer矩形 
– rectForHeaderInSection: //
返回section的header矩形 
10) Registering Nib Objects for Cell
Reuse 
– registerNib:forCellReuseIdentifier:
// 
11) 管理委托和数据源 (重要) 
 
dataSource  property // 通常会这么用: myTableView.delegate = self; self
为viewController 
  delegate  property //
通常会这么用:     myTableView.dataSource = self; self
为viewController

UITableView 常用方法(转自CSDN),布布扣,bubuko.com

时间: 2024-08-09 19:47:17

UITableView 常用方法(转自CSDN)的相关文章

UITableView常用方法

1遵守代理协议 2.设置代理 self.tableView.delegate = self; 3 实现代理方法 //    3.设置分割线的样式 //    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; //    4.设置分割线的颜色 self.tableView.separatorColor = [UIColor blueColor]; /** * 返回多少组 */ - (NSInteger)number

iOS开发-UITableView常用方法

UITableView常用来展示数据,类似于Android中的ListView,相对于Android中的ListView而言,UITableView的实现是非常简单,继承UITableViewDataSource, UITableViewDelegate然后根据需要是实现对应的方法即可. UITableView有两个默认的内置风格,Plain和Grouped,Plain表明表格视图自身没有真正地在你自己实际地提供任何外观之前提供很多的外观,大部分情况下,它会做的唯一的事情是它会给你这些heade

iOS开发-UI (八)TableView

知识点: 1.UITableView使用 2.UITableView分段功能 3.UITableViewCell重用机制 ======================= UITableView使用 1.UITableView作用 2.UITableView创建 - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style; UITableViewStyle: UITableViewStylePlain       列表模式 UIT

学习IOS开发UI篇--UI知识点总结(四) UITabelView/UITableViewCell

UITabelView:常用属性 @property (nonatomic)          CGFloat    rowHeight;             // will return the default value if unset @property (nonatomic)          CGFloat     sectionHeaderHeight;   // will return the default value if unset @property (nonatom

ios开发 UITableViewController

iOS中显示数据列表最常用的一个控件,支持垂直滚动 UITableView的两种内置样式UITableViewStylePlain UITableViewStyleGrouped 数据源(dataSource)和代理(delegate)l? UITableView需要一个数据源(dataSource)来显示数据 ,UITableView会向数据源查询一共有多少行数据以及每一行显 示什么数据等.没有设置数据源的UITableView只是个空壳.凡 是遵守UITableViewDataSource协

tableView 数据源(dataSource)和代理(delegate)

UITableView的数据源(dataSource)和代理(delegate) UITableView需要一个数据源(dataSource)来显示数据,UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等.没有设置数据源的UITableView只是个空壳.凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源. 通常都要为UITableView设置代理对象(delegate),以便在UITableView触发一下事件时做

.NET程序员的iOS面试之旅

最近比较闲,昨天写了一篇ASP.NET程序员的Android学习之旅,算是总结了一下自己的Android学习过程,希望今年能有机会用Android做做项目.Android学习完之后自己买了mac开启iOS学习,年后本来想找个项目看看,出去面试的时候胜算也会大点,不过由于时间的关系,投了一些实习的简历出去,当时心里想的是我做过服务端,Android也懂点,iOS基础水平没问题,找一个实习工作应该可以的吧,但是事与愿违,简历筛选这一关都被拒掉了.不过后来陆陆续续接到一些面试的电话,我大概统计了一下,

2019-12-26

1.mysql sql语句大全 https://www.cnblogs.com/bchjazh/p/5997728.html2.MySQL编程基础 https://wenku.baidu.com/view/37154d635e0e7cd184254b35eefdc8d376ee1429.html3.编写WebSocket客户端 https://blog.csdn.net/wanglui1990/article/details/79306377 4.websocket客户端代码示例 https:/

UITableView的常用方法

UITableView的常用方法 一.UITableView的代理方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #pragma mark 每一行的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath   #pragma mark