首页 > 怎么拿到 UITableView 滑动删除的那个红色的删除按钮对象?

怎么拿到 UITableView 滑动删除的那个红色的删除按钮对象?

iOS 9 的 可以通过下面的代码拿到,self 是 UITableViewCell,但是9以下就不行了,而且这么拿 也不靠谱,有大神知道有什么更好的办法能过拿到这个按钮对象吗?

for (UIView *subView in self.subviews) {
        if ([NSStringFromClass([subView class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
            
            UIView *view = ((UIView *)[subView.subviews firstObject]);
            
        }
    }
    


遍历subviews不一定能拿到你想要的view的,因为有时候,你看到的东西,它不一定是一个view,也许是一个layer,也许只是view里面draw出来的一小块区域。

你想拿到这个view,能想到的就只有定制它的文字或者样式,再者就是添加一些触摸事件了。

修改文字和背景色还有添加点击都很容易做到,如果想深度定制这个view,也许自己实现一个可能会更好。

不如说说拿到这个是想做什么操作呢?说不定可以换个思路。

定制“编辑”“删除”按钮实现tableView的一个delegate:


    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
        // 编辑
        let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "编辑") {
            [unowned self](_, _) -> Void in
            if let vc = self.pushViewController("TUAddSessionViewController") as? TUAddSessionViewController {
                vc.session = TUCache.shared.sessionItems[indexPath.row]
                vc.editMode = true
            }
        }
        // 设置按钮背景色
        editAction.backgroundColor = self.navigationController?.navigationBar.tintColor
          
        // 删除
        let delAction = UITableViewRowAction(style: UITableViewRowActionStyle.Destructive, title: "删除") {
            (_, _) -> Void in
            TUCache.shared.sessionItems.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
        }
        return [delAction, editAction]
    }

你应该是想修改按钮上面的文字吧,那你可以调用下面的API:

func tableView(tableView: UITableView!, editActionsForRowAtIndexPath indexPath: NSIndexPath!) -> [AnyObject]!

它可以让你自定义返回多个按钮,可以设置一个红色的按钮和多个灰色的按钮。

下面这些链接可以参考一下:
http://www.cnblogs.com/scaptain/p/3950123.html
http://blog.csdn.net/lcl130/article/details/42131821

【热门文章】
【热门文章】