Swift: how to make a UIView clickable in UITableViewCell?
for me a sample setup like the following totally works:
class TableViewController: UITableViewController {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath)
}
}
class CustomCell: UITableViewCell {
@IBOutlet weak var bigButton: UIView!
override func awakeFromNib() {
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self, action: Selector("bigButtonTapped:"))
bigButton.addGestureRecognizer(tap)
}
func bigButtonTapped(sender: UITapGestureRecognizer) {
print("bigButtonTapped")
}
}
i did not change any of the default values of userInteractionEnabled
for the view or imageview or label. compare my implementation with yours and see if you maybe forgot something... did you e.g. connect the outlet?
sample project: https://www.dropbox.com/sh/hpetivhc3gfrapf/AAAf6aJ0zhvRINPFJHD-iMvya?dl=0
edit for your project
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCellWithIdentifier("StationsIntroHeader") as! StationsIntroHeader
headerCell.update()
return headerCell
// return headerCell.update().contentView
}
Related videos on Youtube
Daniele B
Updated on September 02, 2022Comments
-
Daniele B about 1 year
Inside a
UITableViewCell
, I am trying to implement a button with both an image and a text.It seems the standard
UIButton
cannot achieve that. So I created aUIView
which contains aUIImageView
and aUILabel
.You can see the implementation here on the right hand side, the "follow trip" button (the "+" is an UIImageView, and "follow trip" is a UILabel)
I am now trying to make such
UIView
(i.e. the button) clickable, but I cannot find a way.This is my implementation, but it doesn't work:
class StationsIntroHeader: UITableViewCell { @IBOutlet weak var bigButton: UIView! override func awakeFromNib() { super.awakeFromNib() let tap = UITapGestureRecognizer(target: self, action: Selector("followTrip:")) bigButton.addGestureRecognizer(tap) } func followTrip(sender:UITapGestureRecognizer) { print("tap working") } }
I have made sure the User Interaction Enabled is ON on the
UIView
and OFF on both theUIImageView
andUILabel
-
SpaceDust__ almost 8 yearsyou can just do a orange image with plus sign as background image for your button and right align the follow trip label, or also you can add a custom button same size as your view and add it to your view with transparent background and no label so you can click it
-
R Menke almost 8 yearsThe touches are being "absorbed" by the
UITableView
. Add a gesture recogniser to theUITableView
instead and use the coordinates to figure out which cell and which button was clicked.
-
-
Daniele B almost 8 yearsHi Andrè! I just run your project, and it works indeed! I am trying now to compare the Interface Builder settings of the controls to spot any difference
-
André Slotta almost 8 yearsIf you do not find what is going wrong, share your project via dropbox - if available - and I take a look!
-
Daniele B almost 8 yearsI can't find the fix! I uploaded a simplified version: dropbox.com/sh/rgfvrn1jpqcfloj/AADEar4aryx08rwb6je5s_mqa?dl=0