美文网首页iOS开发技能程序员iOS Developer
UITableView进阶:常用代理方法及属性

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

作者: 非典型技术宅 | 来源:发表于2016-12-17 19:47 被阅读345次

在前面的文章里面已经写过了“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:https://img.haomeiwen.com/i2248583/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];
      }   
    

    }

    

相关文章

网友评论

本文标题:UITableView进阶:常用代理方法及属性

本文链接:https://www.haomeiwen.com/subject/uigomttx.html