gpt4 book ai didi

ios - menuController Action 的 tableView 设置编辑不起作用

转载 作者:行者123 更新时间:2023-11-30 12:22:54 25 4
gpt4 key购买 nike

我有一个 tableView 和 tableViewCell。我创建 MenuController 来设置 TableView 编辑。但似乎失败了。但我使用导航按钮添加“testFunc”操作。它可以改变多选编辑。我想念什么?我已经添加了tableView的 subview 。我还关注 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.

最佳答案

你想知道的其实是不清楚和模糊的。但我可以在这里看到您正在以错误的方式进行协议(protocol)委托(delegate)。为了使此过程正确,请按照以下步骤操作:

  1. 在您的发送者类中创建一个协议(protocol),您希望从中传递任何消息(在您的情况下:UITalkContent)

    protocol MyProtocol {
    func testFunc()
    }
    class UITalkContent: UILabel {...}
  2. 在发送者类中添加一个委托(delegate) var:

    class UITalkContent: UILabel {
    var delegate: MyProtocol?
    ...
    ...
    }
  3. 使用此委托(delegate)var来调用您在协议(protocol)中指定的方法。这必须在您的发送者类中以及当您准备发送任何消息时完成。在您的情况下,它是 funcforward(menu:)

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

    class ViewController: UIViewController, MyProtocol {
    ...
    ...
    }
  5. 在接收类中实现协议(protocol)的方法。当通知发生时,程序控制将转移到这里。

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

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

关于ios - menuController Action 的 tableView 设置编辑不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44580558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com