table view date selected only the long press - ios

I'm using the tableViewCell in Xib.
In table view, didSelectRowAt function is only called when I long press the tableViewCell
My Code is below
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
categorytabelview.deselectRow(at: indexPath, animated: true)
self.categorylabel.text = totalArrayofCars.object(at: indexPath.row) as? String
categorytabelview.isHidden = true
}
I want to select the tabelViewCell data in single select not in long press.

Since you are using gesture, right before adding the gesture to view, add the following line
tap.cancelsTouchesInView = false // assuming tap is your gesture recogniser variable

Related

How to deselect a UITableView Cell when another one is selected?

I want to deselected a table view cell when another cell is selected. Here is the code that's just deselect the cell for second tap:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
You don't need to call tableView.deselectRow(), just simply set multipleSelection to false.
With Storyboard
Programmatically
tableView.allowsMultipleSelection = false
Select tableView & go to Attributes Inspector & set selection to single selection.
Programatically (inside viewDidLoad() ) you can set as:
self.tableView.allowsSelection = true
self.tableView.allowsMultipleSelection = false

UITableView: how stop selection while swipe the table view cell

I have swipe to delete code here and it my custom TableViewCell I have implemented setSelected method like below ..
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
// tableView.allowsSelectionDuringEditing = false
if tableView.indexPathForSelectedRow != nil, tableView.indexPathForSelectedRow == indexPath {
return UITableViewCell.EditingStyle.none
}
return UITableViewCell.EditingStyle.delete
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
//some code here
}
The logic will do tableview expand collapse based on selection ..but the problem here is if I swipe to delete setSelected also triggers.. not sure how to prevent that any help would be appreciated..
Try this in cellForRow
let cell = tableView.dequeueReusableCell(withIdentifier: "cell_identifier", for: indexPath)
cell.selectionStyle = UITableViewCell.SelectionStyle.none
//or this based on swift version
cell.selectionStyle = .none
return cell
I'm not sure what the appearance you are going for but this will produce the same effect.
If swipe is across the whole screen it will trigger the delete without button press.
If half swipe you can present buttons for user to choose options.
Half swipe (Shows options):
Full swipe (Triggers delete button):
Try adding these two tableView delegate methods for a swipe to delete
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Determine which rows should be editable
return true
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath:
IndexPath) -> [UITableViewRowAction]? {
let button1 = UITableViewRowAction(style: .default, title: "Delete") {
action, indexPath in
print("delete pressed")
// Consider alert to confirm that it was intentionally deleted
}
button1.backgroundColor = UIColor.red
// Create any buttons you want
return [button1]
}
I tried it with and without your tableView method and it seemed to work fine both ways but I don't think your method is necessary if you choose this route
Hope this helps

How to create clickable cells in UITableView (Swift)

I'm working on a personal project and I want to make the cells in my UITableView clickable, such that when they are pressed they segue to the next view, and pass information from their cell along with it when they are pressed.
I've attempted to look over other guides and videos that demonstrate how to do this, however they are all outdated. One that stood out to me that seemed to show promise but didn't end up working was in reference to a certain "didSelectRowAt" function, but I could not get this to work.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as!
CandidateTableViewCell
if (indexPath.row >= candidateCells.count){
print("???")
candidateCells.append(cell)
print("Strange error")
}
tableView.delegate = self
cell.CandidateName?.text = candidatesNames[indexPath.item]
cell.CandidatePos?.text = candidatesPositions[indexPath.item]
//cell.candidateButton.tag = indexPath.row
cell.CandidateName.sizeToFit()
cell.CandidatePos.sizeToFit()
print(candidateCells)
print(indexPath.row)
print(candidateCells.count)
print(indexPath.row >= candidateCells.count)
candidateCells[indexPath.item] = cell
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "Segue", sender: Any?.self)
}
What I was expecting to happen was that the cell would become clickable, and when the cell is clicked it would send me to the next page in the app, however when clicked, nothing happens. The cell does not become highlighted, and the segue does not occur. Thank you so much for any and all suggestions!
If you want to pass information to the next cell, I usually avoid using a Segue, and setup the ViewController with a reuse identifier. By doing this, you can pass information easier. For instance
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "YOURRESUEIDENTIFIER") as! YOURVIEWCONTROLLERCLASS
vc.infoToPass = cellData[indexPath.row]
DispatchQueue.main.async {
self.navigationController?.pushViewController(vc, animated: true)
}
}
Your other option would be to implement the shouldPerformSegueWithIdentifier() method in order to set the data properly. You would click your cell, which calls the performSegue function, which would call the shouldPerformSegueWithIdentifier where you can set the data and return true

Custom tableview cell not deselecting when I return?

I created a custom table view cell. I added a black transparent overlay image for when the user presses the cell. It works great, however, when you go back to the tableview the cell is still selected. Here is my code.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell: PackCell = tableView.cellForRow(at: indexPath)! as! PackCell
selectedCell.highlightImage.isHidden = false
performSegue(withIdentifier: segueIdentifiers[indexPath.row], sender: self)
tableView.deselectRow(at: indexPath, animated: true)
}
Maybe you should also hide your highlightImage when come back?
selectedCell.highlightImage.isHidden = true

How to show/hide a label by pressing a label in swift?

How to show/hide a label by pressing a label in swift?
how to show/hide a button by pressing a button in swift?
how to show/hide a cell by pressing a cell in swift?
thank you.
For the button:
Create an outlet action for the button. In the method type:
'yourButton'.isHidden = true
For the label
Add a tap gesture recognizer to the label. In the acition for it, type:
'yourLabel'.isHidden = true
For the cell
I don't think it is the way to go. It is better if you follow iOS standards, and delete the row by swiping to the left. Like this:
func tableView(_ tableView: UITableView, commit editingStyle:
UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
self.tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
UILabel will need a gesture recognizer to process a tap:
let gesture = UITapGestureRecognizer(target: self, action: "handleTap:")
label1.addGestureRecognizer(gesture)
func handleTap(sender: UITapGestureRecognizer) {
if sender.state == .Ended {
label2.isHidden = true
}
}
UIButtons will just need to be hooked up to an IBAction in the storyboard:
#IBAction func buttonPressed(_ object:AnyObject)
{
button2.isHidden = true
}
Cells cannot be hidden or shown, must be added or removed. Override:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// add or remove items from data source, then
tableView.reloadData()
}

Resources