UITableView

知识点:

1)UITableView

2)UITableViewCell

 

======================================================

 

一、UITableView

 

1、UITableView介绍

UITableView为列表视图,继承UIScrollView

 

2、常用的属性

1)separatorColor  分割线颜色

2)separatorStyle  分割线样式

3)separatorInset 分割线的位置

3)rowHeight  cell的高度,默认为44

4)dataSource  数据源代理

5)delegate  代理

 

3、复用机制

4、常用代理

//设置组的数目

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

//设置每组的cell数目

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

//设置cell的内容

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

//设置cell的高度

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

//当点击cell时会自动调动

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

=====================================================

 

二、UITableViewCell

 

1、UITableViewCell介绍

UITableView上每一行就是一个UITableViewCell,用来显示不同的内容

2、cell的样式

1⃣️. UITableViewCellStyleDefault, // 只有一个label(textLabel正标题)在右边,和imageView在左边

2⃣️. UITableViewCellStyleValue1, // 两个label,左边的label(textLabel正标题)字体默认为黑色并 且向左对齐,右边的label字体(即detailTextLabel副标题)默认为黑色并且向右对齐

3⃣️. UITableViewCellStyleValue2, //两个label,左边的label(textLabel正标题)字体默认为蓝色并 且向右对齐,右边的label字体(即detailTextLabel副标题)默认为黑色并且向左对齐

4⃣️.  UITableViewCellStyleSubtitle // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).上下两个label,上边的label(textLabel正标题)字体默认为灰色并且向左对 齐,下边的label字体(即detailTextLabel副标题)默认为灰色并且向左对齐

 

3、UITableViewCell属性(使用cell中的系统控件时,要注意cell的样式,倘若没有,就算写了也不会显示)

1)textLabel   标题

2)detailTextLabel  副标题

3)imageView 

4)accessoryType  右边的图片样式(默认是UITableViewCellAccessoryNone)设置了样式才会显示

  /**

     UITableViewCellAccessoryNone 没有样式(默认值)

     UITableViewCellAccessoryDisclosureIndicator 箭头

     UITableViewCellAccessoryDetailDisclosureButton 感叹号和箭头

     UITableViewCellAccessoryCheckmark 打钩

     UITableViewCellAccessoryDetailButton 感叹号

     */

5)selectionStyle  点击cell时高亮效果,具体样式如下

  /**

     UITableViewCellSelectionStyleNone

     UITableViewCellSelectionStyleDefault 默认,灰色

     */

6)cell的操作

删除cell

- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

添加cell

- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

移动cell

- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath

刷新

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

4、组头和表头区别

1⃣️.组头或者组尾的显示需要代理的实现

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

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

// 设置组头或者组尾的高度才会调用viewForHeaderInSection或者viewForFooterInSection代理

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

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

2⃣️.UITableViewHeaderFooterView属性

1)textLabel   标题

2)detailTextLabel  副标题

3⃣️.表头(tableHeaderView),表头只有一个,高度由当前创建的view决定,宽度由tableView决定

 

5、tableView其他常用代理

1)返回组头的标题

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

 

2)返回组尾的标题

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

3)  是否允许编辑,默认全部允许编辑

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

 

4)修改删除按钮上的文字

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

 

5)编辑状态,实现这个代理就可以删除或插入(通过判断editingStyle的类型,做出相应的操作)

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

 

6)  // 8.0 的新代理方法

// 同时设置左划cell时出现多个按钮(删除、置顶、更多)

// 如果实现了这个代理方法,自带的删除就没有了

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

//通过以下方法创建按钮

+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(nullable NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;

 

7)移动cell的代理,实现了这个代理才能移动

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

 

8)返回索引

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

     【备注】UITableViewIndexSearch 搜索的图标

 

9)控制索引

    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

     【备注】返回-1,什么都不做

 

 

 

 

 

 

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/Mr-Lin/p/5203588.html