防止 UITableViewCell 子视图上的交互

mat*_*bor 5 cocoa-touch ios swift

我有一个大的 UITableViewCell,里面有 2 个自定义 UIView。其中之一应允许交互(选择和取消选择),而其中之一不应允许交互。

我知道我可以启用或禁用整个单元格或每个子视图上的交互,但如果我禁用 UIView 上的交互,单元格仍然会被选中或取消选择。

例如,有没有办法允许触摸单元格的上半部分而不是底部?

这是我描述的 UITableViewCell 的屏幕截图

在此输入图像描述

Ale*_*ano 1

我完全同意JAL 的观点,你需要检查所有的单元结构,但我也知道有时在某些情况下不可能进行重构。

所以,我认为,假设你有一个CustomTableViewCell由两个视图组成的视图,例如 view1 和 view2:

代码:

class MyView1 : UIView {
    var isTouched : Bool = false
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.isTouched = true
        self.nextResponder()?.touchesBegan(touches, withEvent: event)
    }
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.isTouched = false
        self.nextResponder()?.touchesEnded(touches, withEvent: event)
    }
}
class MyView2 : UIView {
    var isTouched : Bool = false
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.isTouched = true
        self.nextResponder()?.touchesBegan(touches, withEvent: event)
    }
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.isTouched = false
        self.nextResponder()?.touchesEnded(touches, withEvent: event)
    }
}
class CustomTableViewCell: UITableViewCell {
    @IBOutlet weak var myExampleLabel: UILabel!
    @IBOutlet weak var view1: MyView1!
    @IBOutlet weak var view2: MyView2!

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        print("situation: myView1 : \(self.view1.isTouched) myView2: \(self.view2.isTouched)")
        var responder : UIResponder! = self
        while responder.nextResponder() != nil {
           responder = responder.nextResponder()
           if responder is SimpleTableView.ViewController { // the name of your tableView delegate class
                let vc = responder as! ViewController
                let indexPath = vc.tableView.indexPathForCell(self)
                vc.tableView(vc.tableView, didSelectRowAtIndexPath: indexPath!)
            }
        }
        super.touchesBegan(touches, withEvent: event)
    }
}
Run Code Online (Sandbox Code Playgroud)

在另一边,例如,您有 aUIViewController和 a UITableView

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet var tableView: UITableView!
    var items: [String] = ["We", "Heart", "Swift", "So", "Much"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 140.0
        self.tableView.registerNib(UINib(nibName: "CustomTableViewCell", bundle:nil), forCellReuseIdentifier: "CustomTableViewCell")
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell", forIndexPath: indexPath) as! CustomTableViewCell
        cell.myExampleLabel.text = self.items[indexPath.row]
        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell
        print("row = %d",indexPath.row)
        if cell.view1.isTouched { 
           print("tableView: touched view 1") 
           // do whatever you want with cell.view1
           // if you want you can disable cell.view2.userInteractionEnabled
        }
        else {
            print("tableView: touched view 2")
            // same thing for cell.view2
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

一些重要的事情

不要忘记为两个视图设置正确的自定义类,如下图所示:

在此输入图像描述