UImenuController action in tableview not working_swift3

I have a tableView and tableViewCell. I create the MenuController to setTableView editing. But it seems fail. But I use navigation button add action that "testFunc" . it works to change mutiselected editing. What do I miss to think about it?

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)
    }
}

}

this is the label of customized 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
}
}


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 answer

  • answered 2017-06-17 18:08 Ekaterina Belinskaya

    at least you forgot:

    • add your lazy tableView as a subview of ViewController view
    • Your ViewController has to confirm to UITableViewDataSource, UITableViewDelegate

    maybe you've done it already, I just do not see that in your code.