tableViewCell 侧滑(功能botton)

2016-06-21  本文已影响1007人  CoderChou

之前一直想了解这个功能是怎么实现的,但是没有需求就没有动力,今天终于遇到了这个问题,也仔细看了看
1.iOS3.0及以上
在tableViewDelegate 实现以下方法


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        // 删除数据源对应模型
        [self.arrays removeObjectAtIndex:indexPath.row];
        // 从tableView中删除
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];
}

缺点:只能使用系统原生delete,文字,图片皆不可修改。

2.iOS8.0及以上
在tableViewDelegate 实现以下方法

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"你想要修改的文字" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了。。。");
        
        // 收回左滑出现的按钮(退出编辑模式)
        tableView.editing = NO;
    }];
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        //[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        NSLog(@"点击了。。。");
    }];
    
    return @[action0,action1];
    
    
}
如图

3.如果以上还不能满足我们的需求只能使用别人家的了(东西都是人家的好!)

MGSwipeTableCell

上一篇 下一篇

猜你喜欢

热点阅读