Tappable UITableViewCell with an also tappable button in it - uitableview

I have a prototype cell that has some labels on it and a button (well, its actually an imageView, not a button):
I want to achieve this behavior:
Tap on the button executes certain code, say println("foo"), but doesn't perform the "show detail" segue
Tap on the rest of the cell performs a show detail segue
If requirement #1 wasn't necessary, I'd do this:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedPlace = places[indexPath.row]
self.performSegueWithIdentifier("ShowPlaceSegue", sender: self)
}
What is the recommended way to achieve this?
This is not like HTML DOM events? (z-index, etc)
I tried (in a very naif attempt) the following:
class PlaceTableViewCell: UITableViewCell {
#IBOutlet weak var favoritedImageView: UIImageView!
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var administrativeAreaLevel3: UILabel!
func configureCellWith(place: Place) {
nameLabel.text = place.name
administrativeAreaLevel3.text = place.administrativeAreaLevel3
favoritedImageView.addGestureRecognizer(UIGestureRecognizer(target: self, action:Selector("bookmarkTapped:")))
}
func bookmarkTapped(imageView: UIImageView) {
println("foo")
}
}
But no matter if I click the imageView or the rest of the cell, the "show detail" segue is performed and the "foo" isn't printed.
What do you think of putting a UIView, "v", inside the prototype cell that contains the labels and making "v" tappable? something like this:
If I do that, will the cell be grayed while tapped? I'd like to keep that...

Sorry, it was a stupid problem:
The "naif" way was indeed the way to go. Indeed it works like HTML DOM!...
But I changed this:
func configureCellWith(place: Place) {
nameLabel.text = place.name
administrativeAreaLevel3.text = place.administrativeAreaLevel3
favoritedImageView.addGestureRecognizer(UIGestureRecognizer(target: self, action:Selector("bookmarkTapped:")))
}
func bookmarkTapped(imageView: UIImageView) {
println("foo")
}
For this:
func configureCellWith(place: Place) {
nameLabel.text = place.name
administrativeAreaLevel3.text = place.administrativeAreaLevel3
let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("bookmarkTapped:"))
gestureRecognizer.numberOfTapsRequired = 1
favoritedImageView.userInteractionEnabled = true
favoritedImageView.addGestureRecognizer(gestureRecognizer)
}
func bookmarkTapped(sender: UIImageView!) {
println("foo")
}
As you can see, I was using UIGestureRecognizer instead of UITapGestureRecognizer
EDIT:
So, the above is right, but now I think its better to have the action function in the class that contains the tableView, instead of having the action in the cell class itself.
So, i've moved the addGestureRecognizer to the cellForRowAtIndexPath method, ie:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("PlacePrototype", forIndexPath: indexPath) as! PlaceTableViewCell
// Configure the cell
let place = places[indexPath.row]
cell.configureCellWith(place)
// HERE!
let gestureRecognizer = UITapGestureRecognizer(target: self, action:Selector("bookmarkTapped:"))
gestureRecognizer.numberOfTapsRequired = 1
cell.favoritedImageView.userInteractionEnabled = true
cell.favoritedImageView.addGestureRecognizer(gestureRecognizer)
return cell
}
And the action:
func bookmarkTapped(gestureRecognizer: UIGestureRecognizer) {
// println("foo")
var point = gestureRecognizer.locationInView(self.tableView)
if let indexPath = self.tableView.indexPathForRowAtPoint(point)
{
places[indexPath.row].toggleBookmarked()
self.tableView.reloadData()
}
}

Assuming your tableView can be displayed five cells, then the cellForRow will to be called five times, and you will add an UITapGestureRecognizer to five imageView of different. but when you scrolling to the seventh cell, you will got a reused cell(maybe the first cell) in the cellForRow, the imageView of the cell had an UITapGestureRecognizer, if you add UITapGestureRecognizer to the imageView again will cause you tap once trigger multiple times.
You can try this:
class PlaceTableViewCell: UITableViewCell {
#IBOutlet weak var favoritedImageView: UIImageView!
var favoritedTappedBlock: ((Void) -> Void)? // block as callback
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("favoritedImageViewTapped"))
gestureRecognizer.numberOfTapsRequired = 1
favoritedImageView.addGestureRecognizer(gestureRecognizer)
favoritedImageView.userInteractionEnabled = true
}
private func favoritedImageViewTapped() {
if let favoritedTappedBlock = self.favoritedTappedBlock {
favoritedTappedBlock()
}
}
}
And cellForRow:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("PlacePrototype", forIndexPath: indexPath) as! PlaceTableViewCell
// Configure the cell
let place = places[indexPath.row]
cell.configureCellWith(place)
cell.favoritedTappedBlock = {
println("tapped")
}
return cell
}

Related

Does tap gestures added to cell gets preserved on reuse?

I have a cell with an image. I am adding tap gesture to it tableview delegate method. When the cell gets reused, does the tap gesture duplicated? What happens to the tap gesture?
class CalendarCell: UITableViewCell {
#IBOutlet weak var locationImageView: UIImageView!
}
class CalendarViewController: UIViewController {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "calendarCell") as! CalendarCell
let locationLabelTap = UITapGestureRecognizer(target: self, action: #selector(locationDidTap(recognizer:)))
cell.locationLabel.addGestureRecognizer(locationLabelTap)
return cell
}
#objc func locationDidTap(recognizer: UITapGestureRecognizer) {
}
}
Short answer: Yes
Long answer:
You shouldn't be doing it like that. Add the tap gesture when the cell is initialized. This way the tap is added only once when it is created and not everytime it is reused.
class CalendarCell: UITableViewCell {
//Your variable declaration and other stuff
.
.
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//Adding subviews, constraints and other stuff
.
.
.
let locationLabelTap = UITapGestureRecognizer(target: self, action: #selector(locationDidTap(recognizer:)))
locationLabel.addGestureRecognizer(locationLabelTap)
}
.
.
.
}
If you are using a storyboard, you should do the same in awakeFromNib file as pointed out by #DuncanC.
override func awakeFromNib() {
super.awakeFromNib()
.
.
.
let locationLabelTap = UITapGestureRecognizer(target: self, action: #selector(locationDidTap(recognizer:)))
locationLabel.addGestureRecognizer(locationLabelTap)
}

Open URL with a button inside a table view cell

I want to include a button in each table cell that opens a URL.
I've created tables (using an array) with images and labels just fine, however I'm confused how to create a button
Here's what I have so far
class ExploreCell: UITableViewCell {
#IBOutlet weak var exploreImageView: UIImageView!
#IBOutlet weak var exploreTitleView: UILabel!
#IBOutlet weak var exploreDescriptionView: UILabel!
#IBOutlet weak var exploreButton: UIButton!
func setExplore(explore: Explore) {
exploreImageView.image = explore.image
exploreTitleView.text = explore.title
exploreDescriptionView.text = explore.description
exploreButton.addTarget(self, action: "connected:", for: .touchUpInside) = explore.button
}
My Class for the array looks like this
class ExploreListScreen: UIViewController {
#IBOutlet weak var tableView: UITableView!
var explores: [Explore] = []
override func viewDidLoad() {
super.viewDidLoad()
explores = createArray ()
tableView.delegate = self
tableView.dataSource = self
}
func createArray() -> [Explore] {
var tempExplores: [Explore] = []
let explore1 = Explore(image: #imageLiteral(resourceName: "test"), title: "Demo", description: "Essential", button: "")
tempExplores.append(explore1)
return tempExplores
}
Finally I have another file which contains the declared variables
class Explore {
var image: UIImage
var title: String
var description: String
var button: UIButton
init(image: UIImage, title: String, description: String, button: UIButton) {
self.image = image
self.title = title
self.description = description
self.button = button
}
Any advice and guidance would be fantastic. Thank-you!
Here's how I usually solve this. Create a delegate for your UITableViewCell subclass, and set the view controller owning the tableView as its delegate. Add methods for the interactions that happens inside the cell.
protocol YourTableViewCellDelegate: class {
func customCellDidPressUrlButton(_ yourTableCell: YourTableViewCell)
}
class YourTableViewCell: UITableViewCell {
weak var delegate: YourTableViewCellDelegate?
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let button = UIButton()
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
addSubview(button)
}
required init?(coder _: NSCoder) {
return nil
}
#objc func buttonTapped() {
delegate?.customCellDidPressUrlButton(self)
}
}
Then, in the controller, set itself as a delegate and get the indexPath trough the proper method, indexPath(for:)
class YourTableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! YourTableViewCell
cell.delegate = self
return cell
}
}
extension YourTableViewController: YourTableViewCellDelegate {
func customCellDidPressUrlButton(_ yourTableCell: YourTableViewCell) {
guard let indexPath = tableView.indexPath(for: yourTableCell) else { return }
print("Link button pressed at \(indexPath)")
}
}
Then use that indexPath to grab the correct URL and present it from your table viewcontroller with a SFSafariViewController.
Swift 4
This is best way to get indexPath using touchPoint
class YourTableViewController: UITableViewController {
// ...
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SwiftyCell", for: indexPath) as! SwiftyTableViewCell
cell.label.text = "This is cell number \(indexPath.row)"
// WRONG! When cells get reused, these actions will get added again! That's not what we want.
// Of course, we could get around this by jumping through some hoops, but maybe there's a better solution...
cell.yourButton.addTarget(self, action: #selector(self.yourButtonTapped(_:)), for: .touchUpInside)
return cell
}
func yourButtonTapped(_ sender: Any?) {
let point = tableView.convert(sender.center, from: sender.superview!)
if let wantedIndexPath = tableView.indexPathForItem(at: point) {
let cell = tableView.cellForItem(at: wantedIndexPath) as! SwiftyCell
}
}
// ...
}
For more details you can follow this tutorials
Just create UIButton object in viewDidLoad and add this button as a sub view on cell in cellForRowAtIndexPath function. Take Burton's frame as per your requirement.

Adding a gesture recognizer to an image view in a table cell

How can I add a Gesture Recognizer to a UIImageView in a table cell? I want it so that if a user taps an image in the cell, the image will change and the data model will update.
I know this needs to be set up in the UITableViewController. My code currently can execute a command if anywhere in the cell is tapped, but I would like it to execute only if the image is tapped, not anywhere in the cell.
I setup up the gesture recognizer in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Load sample data
loadSampleHabits()
// Initialize tap gesture recognizer
var recognizer = UITapGestureRecognizer(target: self, action: #selector(tapEdit(recognizer:)))
// Add gesture recognizer to the view
self.tableView.addGestureRecognizer(recognizer)
And this is the function
//action method for gesture recognizer
func tapEdit(recognizer: UITapGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.ended {
let tapLocation = recognizer.location(in: self.tableView)
if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) as? HabitTableViewCell {
print("Row Selected")
}
}
}
As a secondary question, are there any conflicts if I want to add a gesture recognizer to the cell and the image view within the cell?
You are adding gesture recognizer on your tableview instead of imageView as you required. Yo need to move your code from viewDidLoad to cellForRowAtIndexPath and add gesture to imageView in each cell while configuing your cell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
var recognizer = UITapGestureRecognizer(target: self, action: #selector(tapEdit(recognizer:)))
// Add gesture recognizer to your image view
cell.yourimageview.addGestureRecognizer(recognizer)
}
Note: Do make sure to enable userinteraction of your image view
cell.yourimageview.userInteractionEnabled = YES;
For your requirement I will suggest using UILongPressGestureRecognizer as it has less chances of conflict in gesture and didselect. Yo can add UILongPressGestureRecognizer in viewDidLoad and access it as per your requirement.
let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.handleLongPress(_:)))
lpgr.minimumPressDuration = 1
tableView.addGestureRecognizer(lpgr)
Define method as
func handleLongPress(_ gesture: UILongPressGestureRecognizer){
if gesture.state != .began { return }
let tapLocation = gesture.location(in: self.tableView)
if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) as? HabitTableViewCell {
print("Row Selected")
}
}
You can try removing if recognizer.state == UIGestureRecognizerState.ended condition from your method.
UITapGestureRecognizer is a discrete gesture, and as such, your event handler is called only once when the gesture was recognized. You don't have to check the state at all. Certainly you won't receive a call for the state of .Began. For more info consider #Rob ans here.
Add This line in cell for row at index path
var recognizer = UITapGestureRecognizer(target: self, action: #selector(tapEdit(recognizer:)))
// Add gesture recognizer to the view
cell.yourimageviewname.addGestureRecognizer(recognizer)
cell.yourimageviewname.userInteractionEnabled = true;
For my suggestion you have to use UIButton in cell, for performance
improvements,
UIButtons
Specially designed for this and have been extensively optimized by Apple for touches.
If you want image in cell you can use UIButton with Image inside.
I have had design a solution like this. I just write a sample code below:
import UIKit
protocol CellImageTapDelegate {
func tableCell(didClickedImageOf tableCell: UITableViewCell)
}
class SampleCell : UITableViewCell {
var delegate : CellImageTapDelegate?
var tapGestureRecognizer = UITapGestureRecognizer()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
private func initialize() {
tapGestureRecognizer.addTarget(self, action: #selector(SampleCell.imageTapped(gestureRecgonizer:)))
self.addGestureRecognizer(tapGestureRecognizer)
}
func imageTapped(gestureRecgonizer: UITapGestureRecognizer) {
delegate?.tableCell(didClickedImageOf: self)
}
}
class ViewController: UITableViewController, CellImageTapDelegate {
// CellImageTapDelegate
func tableCell(didClickedImageOf tableCell: UITableViewCell) {
if let rowIndexPath = tableView.indexPath(for: tableCell) {
print("Row Selected of indexPath: \(rowIndexPath)")
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SampleCellID", for: indexPath) as! SampleCell
cell.delegate = self
return cell
}
}
remember to do following in storyboard
1. enable user interaction of imageview
2. set class of tableviewcell
3. set reuse identifier of tableviewcell
// create an instance of UITapGestureRecognizer and tell it to run
// an action we'll call "handleTap:"
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
// we use our delegate
tap.delegate = self
// allow for user interaction
cell.imageViewName.userInteractionEnabled = true
// add tap as a gestureRecognizer to tapView
cell.imageViewName.addGestureRecognizer(tap)
import UIKit
class UserInfoCell: UITableViewCell{
#IBOutlet weak var imagePlaceholder: UIImageView!
}
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
#IBOutlet weak var tableView: UITableView!
let imagePicker = UIImagePickerController()
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserInfoCell" ,for: indexPath ) as! UserInfoCell
let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.openGallery))
cell.imagePlaceholder.addGestureRecognizer(recognizer)
recognizer.numberOfTapsRequired = 1
cell.imagePlaceholder.isUserInteractionEnabled = true
cell.name.text = "Akshay"
if let data = UserDefaults.standard.data(forKey: "savedImage") {
cell.imagePlaceholder.image = UIImage(data: data as Data)
}
return cell
}
#objc func openGallery(){
imagePicker.sourceType = .photoLibrary
present(imagePicker,animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let userimage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
let imageData = userimage.jpegData(compressionQuality: 1)!
UserDefaults.standard.setValue(imageData, forKey: "savedImage")
print("image found")
self.imagePicker.dismiss(animated: true, completion: nil)
self.tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
tableView.tableFooterView = UIView()
}
}
This code select image from gallery using Tapgesture of ImageView inside a TableViewCell

Passing current UITableViewCell swipe to parent UITableView

I have a custom UITableViewCell, which has a functionality to be swiped to the right. The swipe gesture is based on the translation of x-axis, so, when the translation of x is exceeded 40 points, I want to trigger a segue.
I think this is a perfect place to use delegates to pass data about the current X value. So, I have created a protocol with a function didSwipeCell(), but I'm not sure how to pass the current x value to the UITableView.
Please let me know how to do it, and if you need any extra info, please let me know in the comment instead of downvoting.
Add x value as a parameter of didSwipeCell() method
protocol TableViewCellDelegate {
func didSwipeCell(on xValue: Float)
}
Add the delegate instance to your cell
class TableViewCell: UITableViewCell {
weak var delegate: TableViewCellDelegate?
}
Then, call the method when user swipes the cell giving xValue
delegate?.didSwipeCell(on: xValue)
In your UITableView implement TableViewCellDelegate method.
class TableView: UITableView, TableViewCellDelegate {
func didSwipeCell(on xValue: Float) {
//here you can get the xValue
}
And, do not forget to set the delegate of TableViewCell in your cellForRowAtIndexPath method
cell.delegate = self
You can get value of x simply from your UItableViewCell Class to your UIViewController. Declare closure
var getValue: ((_ xValue: CGFloat)->())!
in your UItableViewCell Class and implement it into your UIViewController Class.
In ViewController
cell. getValue = { (xValue) in
// do what you want to do
}
Here is another way to do it...
class SwipeTableViewCell: UITableViewCell {
var didSwipeAction : ((CGFloat)->())?
var startLocation = CGPoint.zero
var theSwipeGR: UISwipeGestureRecognizer?
func respondToSwipeGesture(_ sender: UIGestureRecognizer) {
if let swipe = sender as? UISwipeGestureRecognizer {
if (swipe.state == UIGestureRecognizerState.began) {
startLocation = swipe.location(in: self.contentView);
} else if (swipe.state == UIGestureRecognizerState.ended) {
let stopLocation = swipe.location(in: self.contentView);
let dx = stopLocation.x - startLocation.x;
if dx > 40 {
// pass the ending X coordinate in the callback
didSwipeAction?(stopLocation.x)
}
}
}
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
myInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
myInit()
}
func myInit() -> Void {
if theSwipeGR == nil {
let g = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(_:)))
g.direction = UISwipeGestureRecognizerDirection.right
self.contentView.addGestureRecognizer(g)
theSwipeGR = g
}
}
}
and then your table view cell setup becomes:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SwipeCell", for: indexPath) as! SwipeTableViewCell
cell.didSwipeAction = {
(xValue) in
print("Simple", indexPath, "X =", xValue)
// call performSegue() or do something else...
}
return cell
}

TableViewCell is not clickable with one finger tap, but it is with two fingers

I created a table view and the tableViewCell is not clickable with one finger, but when I try to click the tableViewCell with two fingers the click event takes place. I don't know why this occurres. I created a custom cell in tableView.
InviteVC
import UIKit
class InvitePeopleVC: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
var nameArray = ["Alwin Lazar", "Ajith Ramesh CR", "Ebrahim KK", "Vishnu Prakash"]
var emailArray = ["alwin#xeoscript.com", "ajith#xeoscript.com", "ebrahim#xeoscript.com", "vishnu#xeoscript.com"]
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var doneImg: UIImageView!
#IBOutlet weak var nameTextFld: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
delegates()
uiModifications()
gestureRecognizers()
}
func delegates() {
tableView.dataSource = self
tableView.delegate = self
nameTextFld.delegate = self
}
func uiModifications() {
nameTextFld.attributedPlaceholder = NSAttributedString(string: "Name or email address", attributes: [NSForegroundColorAttributeName: UIColor.white])
}
func gestureRecognizers() {
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(InvitePeopleVC.dismissKeyboard)))
self.doneImg.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(InvitePeopleVC.doneImgPressed)))
}
func dismissKeyboard() {
nameTextFld.resignFirstResponder()
}
func doneImgPressed() {
print("done Image tapped")
}
func inviteBtnPressed() {
print("invite button pressed")
}
// UITextFieldDelegate method
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == self.nameTextFld {
self.nameTextFld.resignFirstResponder()
}
return true
}
// TableView DataSource methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "InviteCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! InviteCell
cell.nameLbl.text = nameArray[indexPath.row]
cell.emailLbl.text = emailArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
// TableView Delegate methods
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("selected row is \(indexPath.row)")
}
#IBAction func backBtnPressed(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
#InviteCell
import UIKit
class InviteCell: UITableViewCell {
#IBOutlet weak var nameLbl: UILabel!
#IBOutlet weak var emailLbl: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
}
UIViewController Images
TableView Attributes Inspector
InviteCell Atribute Inspector
In the code above, I'm trying to select a cell with one finger, but the selection does not happen.
Thanks in advance...
A more elegant way of dealing with the tap issue is:
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(AppController.dismissKeyboard))
view.addGestureRecognizer(tap)
//this is the KEY of the fix
tap.cancelsTouchesInView = false
This way you can keep your gesture recognizer and still get the table view action in one single tap/selection.
You have the following line in your set up code:
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(InvitePeopleVC.dismissKeyboard)))
That sets up a gesture recognizer for your whole view and that would swallow any touches on the main view. If you remove that, you should get the table cell selection working correctly :)
The Tap gesture you have added in the code is causing the issue. Tapgesture recogniser is listening to the user tap actions in the view. The cell select listner is being blocked by the added Tap gesture.
As #Fahim said, if you remove the tap gesture from your code, then cell selection will work smoothly.

Resources