我有一個tableView和tableViewCell。我創建MenuController來設置TableView編輯。但它似乎失敗了。但我使用導航按鈕添加「testFunc」動作。它可以改變mutiselected編輯。我想念什麼?我已經添加了tableView的子視圖。我也遵循UITableViewDataSource,UITableViewDelegate。tableView設置編輯menuController操作不起作用
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年6月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()
}
}
你好,我@nayem的UITalkContent例如在TableViewCell寫。我應該怎麼做第6步?我也更新了關於tableviewCell的問題。 – JimmyLee
好的。你有一個非常複雜的實現。你想實現什麼目標? ___你的標籤應該告訴你的ViewController嗎?___ – nayem
我覺得它也很複雜。我添加GestureRecognizer標籤,然後顯示menuController。點擊菜單操作讓TableView Mutiselected。我更新圖片。 – JimmyLee