ios UITableView 方法详解

**UITableView** : UIScrollView

1.创建一个UITableView对象

UITableView *tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];

2.separatorColor

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

3.rowHeight

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

4.reloadData

刷新数据     [tableView reloadData];

5.

两个必须实现的方法 

  1).控制一个section中cell 的多少

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

2).控制cell中的内容

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

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;

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**

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**

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

这里涉及到自定义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;

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

推荐阅读更多精彩内容