ios - 怎么拿到 UITableView 滑动删除的那个红色的删除按钮对象?
巴扎黑
巴扎黑 2017-04-17 16:39:12
0
2
645

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

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

巴扎黑
巴扎黑

reply all(2)
刘奇

遍历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]
    }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!