UITableView方法总结(很全面)

UITableView : UIScrollView <NSCoding>

1.创建一个UITableView对象
UITableView *tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];

2.separatorColor
分割线颜色
e.g. tableView.separatorColor = [UIColor redColor];

3.rowHeight
调整每个cell 点高度(默认 44)
e.g. tableView.rowHeight = 60;

4.reloadData
刷新数据
e.g. [tableView reloadData];

5.<UITableViewDelegate,UITableViewDataSource>
两个必须实现的方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
控制一个section中cell 的多少
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)
indexPath
控制cell中的内容

6.选中cell时候使用的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

7.取消选中时候用的方法 (不常用)
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

8.控制分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

9.section上Header显示的内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

10.section上Footer显示的内容
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

11.section顶部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

12.cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

13 该方法返回值用于在表格右边建立一个浮动的索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;

cell相关:
1.返回表格中指定indexPath对应的cell
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
2.返回指定cell的indexPath
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;
3.返回表格中指定点所在的indexPath
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
4.返回表格中指定区域内所有indexPath 组成的数组
- (NSArray *)indexPathsForRowsInRect:(CGRect)rect;
5.返回表格中所有可见区域内cell的数组
- (NSArray *)visibleCells;
6.返回表格中所有可见区域内cell对应indexPath所组成的数组
- (NSArray *)indexPathsForVisibleRows;
7.控制该表格滚动到指定indexPath对应的cell的顶端 中间 或者下方
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

8.控制该表格滚动到选中cell的顶端 中间 或者下方
-(void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

**处理单元格的选中**
1.@property(nonatomic) BOOL allowsSelection
控制该表格是否允许被选中
2.@property(nonatomic) BOOL allowsMultipleSelection
控制该表格是否允许多选
3.@property(nonatomic) BOOL allowsSelectionDuringEditing;
控制表格处于编辑状态时是否允许被选中
4.@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing
控制表格处于编辑状态时是否允许被多选

5.获取选中cell对应的indexPath
- (NSIndexPath *)indexPathForSelectedRow;
6.获取所有被选中的cell对应的indexPath组成的数组
- (NSArray *)indexPathsForSelectedRows
7.控制该表格选中指定indexPath对应的表格行,最后一个参数控制是否滚动到被选中行的顶端 中间 和底部
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
8.控制取消选中该表格中指定indexPath对应的表格行
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

<UITableViewDelegate>
9.当用户将要选中表格中的某行时触发方法
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
10.当用户完成选中表格中的某行时触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
11.当用户将要取消选中表格中某行时触发
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
12.当用户完成取消选中表格中某行时触发
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath

关于对表格的编辑
1.对表格控件执行多个连续的插入,删除和移动操作之前调用这个方法开始更新
- (void)beginUpdates;
2.对表格控件执行多个连续的插入,删除和移动操作之后调用这个方法结束
- (void)endUpdates;
3.在一个或多个indexPath处插入cell
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
4.删除一个或多个indexPath处的cell
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
5.将制定indexPath处的cell移动到另个一indexPath处
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath*)newIndexPath
6.指定的indexSet所包含的一个或多个分区号对应的位置插入分区
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
7.删除指定indexSet所包含的一个或多个分区号所对应的分区
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
8.将指定分区移动到另一个位置
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection

**@protocol UITableViewDataSource<NSObject>**

9.该方法返回值决定指定indexPath对应的cell是否可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath;
10.该方法返回值决定指定indexPath对应的cell是否可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath;
11.当用户对指定表格行编辑(包括插入和删除)时触发该方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath;
12.该方法触发移动  通常对数据进行处理(重要)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

**@protocol UITableViewDelegate<NSObject,UIScrollViewDelegate>**

13.开始/完成 编辑时调用的两个方法
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath*)indexPath;
14.该方法返回值决定了 indexPath处的cell 的编辑状态  返回值为枚举类型 分别为 None Delete Insert
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
15.该方法决定了 cell处于被编辑状态时是否应该缩进  若未重写 所有cell处于编辑状态时都会缩进
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;

**UITableViewCell** : UIView <NSCoding, UIGestureRecognizerDelegate>
这里涉及到自定义UITableViewCell 以下为具体步骤以及需要注意到地方
1.首先创建一个类继承UITableViewCell
2.把自定义cell上到自定义视图全部设置为属性(注意:属性名一定不要和系统属性命重复 e.g.  imageView,textLable,detailTextLable)
3.在cell的初始化方法中 对自定义视图对属性初始化,在初始化对时候可以不指定frame(注意,这里加载到视图上时 加载到contentView 上  同时注意内存管理)
4.在layoutSubviews方法中完成最后操作 通常给出frame(注意,这个方法为系统自带方法,当一个cell显示到屏幕上之前,最后调用到一个方法, 所有cell到操作 包括赋值,调整高度等 都已经完成  一定不要忘记[super layoutSubviews];)

附加:当一个cell被选中的方法
 - (void)setSelected:(BOOL)selected animated:(BOOL)animated

一些小操作:
//将单元格的边框设置为圆角
cell.layer.cornerRadius = 12;

cell.layer.masksToBounds = YES;

** 再次声明本文转自:http://blog.csdn.net/qq11231325**


update:以下内容为自己添加

  1. 选中tableView中的某一行

     - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;//定位到tableView的某一行
     //示例
     NSIndexPath *ip=[NSIndexPath indexPathForRow:0 inSection:0];
     [self.listView.tabView selectRowAtIndexPath:ip animated:NO scrollPosition:UITableViewScrollPositionNone];//选中第一分区第一行
    
  2. 待更新

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,738评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,377评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,774评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,032评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,015评论 5 361
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,239评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,724评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,374评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,508评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,410评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,457评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,132评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,733评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,804评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,022评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,515评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,116评论 2 341

推荐阅读更多精彩内容