UITableView进阶:常用代理方法及属性

在前面的文章里面已经写过了“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

//选中cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

2.2 编辑模式

  • 开启支持编辑模式
Paste_Image.png
// 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
//修改上图的图标
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

  • 修改上图图标对应的执行方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

2.3 拖拽排序模式

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

Paste_Image.png
  • 开启拖拽模式
// 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;
}
  • 拖拽之后对应的执行方法
// 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 @"删除";
    }
##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

##2.5 修改cell左滑文字
  • (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"haha";
    }
#3. 四种刷新tableView的方法

// 新增表格数据
[tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop];

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

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

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

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

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

[self.contactArray removeObjectAtIndex:fromIndexPath.row];
[self.contactArray insertObject:tempContact atIndex:toIndexPath.row];
   
##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];
      }   
    

    }

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

推荐阅读更多精彩内容