前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UITableView进阶:常用代理方法及属性1. 常用属性2. 进阶的常用代理方法

UITableView进阶:常用代理方法及属性1. 常用属性2. 进阶的常用代理方法

作者头像
stanbai
发布2018-06-28 13:26:45
1.1K0
发布2018-06-28 13:26:45
举报
文章被收录于专栏:非典型技术宅

在前面的文章里面已经写过了“UITableView基础”,所以这篇里面就不再对这里面的内容进行介绍。如果有幸去瞄一眼的,可以通过文章下面的拓展链接传送门去看。&

今天重点聊一聊UITableView中经常使用到的代理方法及属性。本文不是工具箱,所以不会将所有的属性和方法都写下来噢。只是总结经常使用到的。太完整的也记不住,真的是要用的时候临时翻一下.h文件看看也行。

1. 常用属性

1.1 分隔线属性

属性名称

数值

作用

separatorStyle

UITableViewCellSeparatorStyle

分割线样式

separatorColor

UIColor

分隔线颜色

1.2 cell被选中的属性

属性名称

数值

作用

allowsSelection

BOOL

允许选中

allowsMultipleSelection

BOOL

允许多选

indexPathsForSelectedRows

NSArray < NSIndexPath *>

获取当前选中cell的indexPaths

indexPathsForVisibleRows

NSArray < NSIndexPath *>

当前可见行数

  • allowsSelection:BOOL类型,一是说说这一个cell是否可以被选中。在某种情况下,我们希望点击cell的时候不需要做出任何的反应,就可以修改这个属性。
  • allowsMultipleSelection:需要进行多行选择的时候就要将此设置为YES
  • indexPathsForSelectedRows:这里返回的是包含了indexPath的数组噢,因为要考虑到是多行选中的情况。知道了这个属性之后,不要一说获取选中的cell的indexPath就只会用代理方法。&
  • indexPathsForVisibleRows:这个方法其实并不太经常使用,但是很能提升逼格。这个属性也是一个数组,它装着目前屏幕上可见的cell的indexPath集合。在做两级菜单联动的时候可能会需要用到。

2. 进阶的常用代理方法

神马滚动到指定的cell,设置cell的高度,设置header、footer的高度等等这些方法就不再说了。

2.1 最最常用的方法:选中指定的cell

代码语言:javascript
复制
//选中cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

2.2 编辑模式

  • 开启支持编辑模式

Paste_Image.png

代码语言:javascript
复制
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
  • 修改点击编辑后,每个cell前方的icon
代码语言:javascript
复制
//修改上图的图标
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  • 修改上图图标对应的执行方法
代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

2.3 拖拽排序模式

重要:开启支持拖拽排序的前提是:开启支持编辑模式

Paste_Image.png

  • 开启拖拽模式
代码语言:javascript
复制
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
  • 拖拽之后对应的执行方法
代码语言:javascript
复制
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    ```

//修改删除按钮文字

  • (NSString *) tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"删除"; }
代码语言:javascript
复制
##2.4 自定义cell左滑事件

![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2248583-728eb64caa005450.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  • (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
代码语言:javascript
复制
##2.5 修改cell左滑文字
  • (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ return @"haha"; }
代码语言:javascript
复制
#3. 四种刷新tableView的方法
代码语言:javascript
复制
// 新增表格数据
[tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop];

// 删除表格数据
[tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];

// 局部刷新指定的行
[tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];

// 刷新全部表格数据,如果能够准确确定被修改的数据行,就不要用此方法
[tableView reloadData];
代码语言:javascript
复制
#4. tableViewCell排序
##4.1 cell交换排序
- 在cell拖拽对应的执行方法中进行。

[self.contactArray exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];

代码语言:javascript
复制
##4.2 cell顺序排序
- 依旧还是在cell拖拽对应的执行方法中进行。
代码语言:javascript
复制
//     获取选中的数组。删除后,插入到相应的行
GMContact *tempContact = self.contactArray[fromIndexPath.row];

[self.contactArray removeObjectAtIndex:fromIndexPath.row];
[self.contactArray insertObject:tempContact atIndex:toIndexPath.row];
代码语言:javascript
复制
##4.3 开了编辑模式后,在编辑模式下插入一条cell
- 需要在编辑模式下,修改icon执行方法中写入。
  • (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //删除数组对应的数据 [self.contactArray removeObjectAtIndex:indexPath.row]; //删除对用cell [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { //插入数据 GMContact * contact = [[GMContact alloc] init]; contact.name = @"曲大帅帅"; contact.number = @"110119120"; //往数组中插入 [self.contactArray insertObject:contact atIndex:indexPath.row + 1]; //插入cell NSIndexPath * inserIndex = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0]; //插入动画,不然显得很突兀 [tableView insertRowsAtIndexPaths:@[inserIndex] withRowAnimation:UITableViewRowAnimationFade]; } }
代码语言:javascript
复制
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016.12.17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 常用属性
    • 1.1 分隔线属性
      • 1.2 cell被选中的属性
      • 2. 进阶的常用代理方法
        • 2.1 最最常用的方法:选中指定的cell
          • 2.2 编辑模式
            • 2.3 拖拽排序模式
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档