UITableViewCell

  • 取消UITableViewCell选中的背景颜色

1.方法一:
在TableView DataSource的- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法里直接调用[tableView deselectRowAtIndexPath:indexPath animated:NO];就可以了。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

2.方法二:
在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法里面加上:cell.selectionStyle = UITableViewCellSelectionStyleNone;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       UITableViewCell *cell = [[UITableViewCell alloc] init];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;

}

参考来自简书中的文章:
原文链接:http://www.jianshu.com/p/e37e729935f6

  • 自动计算Cell的高度

注意:一定要设置预估行高,才能自动计算高度

// 告诉tableView的真实高度是自动计算的,根据你的约束来计算
self.tableView.rowHeight = UITableViewAutomaticDimension;
// 告诉tableView所有cell的估计行高
self.tableView.estimatedRowHeight = 44
// 返回估算告诉,作用:在tablView显示时候,先根据估算高度得到整个tablView高,而不必知道每个cell的高度,从而达到高度方法的懒加载调用
Simulator Screen Shot 2016年11月11日 下午2.50.53.png
  • 让Cell进入编辑状态

1.实现右滑删除
只要实现了tableview的这个代理方法就能出现右滑删除的按钮

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        NSLog(@"添加");
    }
}
Simulator Screen Shot 2016年11月11日 下午2.52.03.png

2.实现左边出现添加或删除按钮
1、设置tableview的编辑状态为YES

[self.tableView setEditing:!self.tableView.editing animated:YES];

2、 事项tableview的代理方法

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
/**
 *   UITableViewCellEditingStyleNone,
 *   UITableViewCellEditingStyleDelete,
 *   UITableViewCellEditingStyleInsert
 */
    return UITableViewCellEditingStyleInsert;
}
Simulator Screen Shot 2016年11月11日 下午2.52.29.png
  • 刷新TableView

1.整体刷新:[self.tableview reloadDate];
2.局部刷新:(条件:行数不变)

// 删除一行
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

// 添加一行
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];

// 删除或添加模型数据

参考文章:【iOS_成才录】iOS -- UITableView基本使用【ljloving】UITableView可编辑状态常用操作

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容