I totally agree with JAL, you need to review all your cell structure, but I know also that sometimes and in some cases it's impossible doing refactor.
So , I think this, suppose you have a CustomTableViewCell
composed by two views , named for example view1 and view2:
The code:
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 {
let vc = responder as! ViewController
let indexPath = vc.tableView.indexPathForCell(self)
vc.tableView(vc.tableView, didSelectRowAtIndexPath: indexPath!)
}
}
super.touchesBegan(touches, withEvent: event)
}
}
To the other side, where you have for example a UIViewController
with 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")
}
else {
print("tableView: touched view 2")
}
}
}
Some important things:
Don't forget to set the correct Custom Class for the two views like explained in this picture:
