iOS开发技能程序员iOS Developer

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

2016-12-17  本文已影响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 *> 当前可见行数

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;
}
//修改上图的图标
- (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 {
    ```

//修改删除按钮文字

##2.4 自定义cell左滑事件

![Paste_Image.png](http:https://img.haomeiwen.com/i2248583/728eb64caa005450.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    

##2.5 修改cell左滑文字
#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执行方法中写入。
   
    
上一篇 下一篇

猜你喜欢

热点阅读