tableView设置编辑menuController动作不起作用

时间:2017-06-16 03:57:26

标签: ios swift uitableview uimenucontroller

我有一个tableView和tableViewCell。我创建了MenuController来进行setTableView编辑。但似乎失败了。但我使用导航按钮添加“testFunc”动作。它可以改变mutiselected编辑。我怎么想念它呢?我已经添加了tableView的子视图。我还遵循UITableViewDataSource,UITableViewDelegate。

protocol DelegateA {
func testFunc()
}

class ViewController: UIViewController,DelegateA { 

lazy var tableView:UITableView = { ()->UITableView in
    let ui:UITableView = UITableView()
    ui.delegate = self
    ui.dataSource = self
    ui.separatorStyle = .none
    ui.backgroundColor = defaultBackgroundColor
    return ui
}()

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.allowsMultipleSelectionDuringEditing = true
}

func testFunc() {

    print("123") //it print "123" successful

    //not working
    if(self.tableView.isEditing == false) {
        self.tableView.setEditing(true, animated:true)
    }
    else {
        self.tableView.setEditing(false, animated:true)
    }
}

}

这是自定义tableViewCell的标签

class UITalkContent:UILabel {

var delegate:DelegateA?

override init(frame: CGRect) {
    super.init(frame:frame)
    translatesAutoresizingMaskIntoConstraints = false
    numberOfLines = 0
    isUserInteractionEnabled = true
    addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(gestureLongPress)))
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func drawText(in rect: CGRect) {
    let insets: UIEdgeInsets = UIEdgeInsets(top: defaultContentPadding, left: defaultPadding, bottom: defaultContentPadding, right: defaultPadding)
    super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}

func gestureLongPress(sender:UILongPressGestureRecognizer) {

    if (sender.state == .ended) {
        return
    }

    becomeFirstResponder()


    let menu = UIMenuController.shared


    menu.menuItems = [
        UIMenuItem(title: localString(string: "FORWARD"), action: #selector(forward)),
    ]


    menu.setTargetRect(bounds, in: self)


    menu.setMenuVisible(true, animated: true)
}

func forward(menu :UIMenuController ) {
    print("321") //print "321" successfully 
    self.delegate = ViewController()
    self.delegate?.testFunc()

}

override var canBecomeFirstResponder: Bool{
    return true
}

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if (action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(forward)) {
        return true
    }
    return false
}

override func copy(_ sender: Any?) {
    UIPasteboard.general.string = text
}
}

2017.06.16更新

class ContentTableViewCell: UITableViewCell {

var labelContent:UILabel = { ()->UILabel in
    let ui:UILabel = UITalkContent()
    ui.textColor = UIColor(red:0.20, green:0.20, blue:0.20, alpha:1.00)
    ui.backgroundColor = UIColor.white
    ui.font = defaultTextFont
    ui.numberOfLines = 0
    ui.lineBreakMode = NSLineBreakMode.byCharWrapping
    ui.layer.cornerRadius = defaultButtonRadius
    ui.layer.masksToBounds = true
    ui.isHidden = false
    return ui
}()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    loadContent()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func layoutSubviews() {
    super.layoutSubviews()
    loadVFL()
}

func loadContent() {
    contentView.addSubview(labelContent)
}

func loadVFL() {
    labelContent.frame = CGRect(x: 0, y: 0, width: contentWidth, height: CGFloat.greatestFiniteMagnitude)
    labelContent.sizeToFit() 

}
}

mutiselected tableview like this.

1 个答案:

答案 0 :(得分:0)

你想知道的事实上是不清楚和模糊的。但我可以在这里看到 您正在以错误的方式protocol-delegation 。要使此过程正确,请按照以下步骤操作:

  1. 在您的发件人类中创建一个协议,您希望从中传递任何邮件(在您的情况下:UITalkContent

    protocol MyProtocol {
        func testFunc()
    }
    class UITalkContent: UILabel {...}
    
  2. 在您的发件人类中添加delegate var

    class UITalkContent: UILabel {
        var delegate: MyProtocol?
        ...
        ...
    }
    
  3. 使用此delegate var 调用您在协议中指定的方法。这必须在您的发件人类中以及准备好发送任何邮件时完成。在你的情况下,它是func forward(menu:)

    class UITalkContent: UILabel {
        ...
        ...
        func forward(menu :UIMenuController ) {
            ...
            ...
            delegate.testFunc()
        }
        ...
        ...
    }
    
  4. 在接收课程中采用协议(在您的情况下:ViewController

    class ViewController: UIViewController, MyProtocol {
        ...
        ...
    }
    
  5. 在接收类中实现协议的方法。通知发生时,程序控制将在此处传输。

    class ViewController: UIViewController, MyProtocol {
        ...
        ...
        func testFunc() {
            // do what you want to do when program control reaches here
        }
        ...
        ...
    }
    
  6. 现在您必须将接收类设置为该发件人类的delegate。为此,您必须将self(接收类)分配给sender类的实例。 (但我没有看到您UILabel)的任何实例

    class ViewController: UIViewController, MyProtocol {
        ...
        ...
        `instance of UILabel` = self
        ...
        ...
    }