UITableView实战总结(二)——点击状态与删除样式

一、点击时状态

1、不显示选中状态

cell.selectionStyle = UITableViewCellSelectionStyleNone;

2、点击时的颜色

typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
    UITableViewCellSelectionStyleNone,    // 无色
    UITableViewCellSelectionStyleBlue,    // 蓝色
    UITableViewCellSelectionStyleGray,    // 灰色
    UITableViewCellSelectionStyleDefault NS_ENUM_AVAILABLE_IOS(7_0)
};

二、点击时动画

1、正常点击(选中的图层不覆盖图片)

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

2、特殊点击(选中的图层覆盖图片)

/** 在Cell中进行设置 */
// 申明选中时覆盖的view
@property (strong, nonatomic) UIView *selectView;

// 对高亮状态进行设置
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    if (highlighted) {
        _selectView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
    }
    else {
        _selectView.backgroundColor = [UIColor clearColor];
    }
}

三、侧滑删除样式

1、系统删除样式

/** 设置Cell为可编辑 */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

/** 返回编辑方式为删除 */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

/** 侧滑删除按钮 执行 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 执行删除操作
}

/** 设置title文字 */
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

2、自定义删除样式

/** 侧滑删除按钮 */
- (NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
                                                                         title:@"删除"
                                                                       handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                                                                           [self tableView:tableView
                                                                        commitEditingStyle:UITableViewCellEditingStyleDelete
                                                                         forRowAtIndexPath:indexPath];
                                                                       }];
    rowAction.backgroundColor = [UIColor lightGrayColor];    // 设置颜色
    NSArray *arr = @[rowAction];
    return arr;
}

/** 侧滑删除按钮 执行 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 执行删除操作
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,239评论 25 709
  • 一、工作区和工作流程 1.工作区和面板 保存、删除、重置工作区停靠、编组、浮动面板 2.首选项 常规、预览、导入、...
    朱细细阅读 12,439评论 0 52
  • 文/傻二 所有失去的,都会以另一种方式回归 2017年11月01日 星期三 天气晴 ① 多希望这是一个噩梦 清...
    咿呀鱼阅读 4,940评论 0 0
  • 1 你相信命运吗?我相信这不是命运。一切的主宰者是我自己。现在能做到的就是不住叹气。遗憾不住叩响心头。闷响的回声一...
    沈轻舟一阅读 1,765评论 4 2
  • 一、text-align:center的作用是什么?作用在什么元素上?能让什么元素水平居中? text-align...
    青鸣阅读 2,953评论 0 0

友情链接更多精彩内容