I have a situation to need only call didSelectRowAt.
when I click twice on a cell at first it calls didSelectRowAt, in second click it calls didDeselectRowAt. However, I need call didSelectRowAt in both clicks.
I don't want to use this solution:
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
// call select here
}
Because it is too complicated in my codes and it is not very clean & clear.
How can I prevent call didDeselectRowAt instead call didSelectRowAt?
In your didSelectRowAtIndexPah, deselect the row
tableView.deselectRow(at: indexPath, animated: false)
Related
I am trying enable editing of my UITableView by providing an Edit button on the nav bar.
The intention is to allow the user to insert new items and delete existing items.
When clicking the Edit button iOS shows the "Delete" decoration (deleting works fine), but doesnt show "Insert"
I have added navigationItem.rightBarButtonItem = editButtonItem to my UIViewControllers viewDidLoad and have added the following methods to my UITableViewDataSource delegate:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
AppDelegate.persistenceContext.delete(self.fetchedResultsController.object(at: indexPath))
AppDelegate.saveContext()
} else if editingStyle == .insert {
let current = self.fetchedResultsController.object(at: indexPath)
// do rest of insertion logic
}
}
What am I missing something?
Oh and out of interest, if this is possible would the new cell be inserted above or below the one on which the action was initiated?
For the record I can add custom swipe actions to the cells, but the users feedback is they prefer an edit button.
Any light would be appreciated, all the Googling has gotten me is using a button to insert a new row, or adding custom swipe actions.
You need to implement the delegate method:
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle
Return .delete, .insert, or .none for a given index path. Normally only one row (typically first or last) would return .insert and the rest would return .delete. Return .none for any row that can't be inserted or deleted.
It's not uncommon that you also need to override the setEditing(_ editing: Bool, animated: Bool) method to add/remove an extra row just for the purposes of showing the green + (.insert) icon.
An alternative is to show the + icon in the nav bar instead of having a row with the insert icon.
I have a ViewController that contains a collectionview and a tableview. I would like to be able to press a row in my tableView and update the collectionView with new images. I'm not sure how I can reference the collectionView in this scenario.
I'm assuming I would have to put some code in my "DidSelectRowAt" method in my tableView, and pass data using dictionaries. Anyone have any idea? thanks!
Try this method and pass your latest dictionary to collectionView Dictionary
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
yourCollectionViewDict = arrDict[indexPath.row]
collectionView.reloadData()
}
Have you tried triggering collectionView.reloadData() from your tableView's didSelectRowAtIndexPath?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
collectionView.reloadData()
}
I have implemented two custom cells in the tableview.Now I want to replace these two custom cells with one another. How to achieve this?
Implement following tableview methods and write your code in it
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// return boolean value for a specific or all cells, you want to enable movement
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// Handle array element movement along with your row/cell
}
Share your full source code better help
If you would like to reorder rows in UITableView. Take a look at two delegates that are implemented in the UITableView.
They are: tableView:canMoveRowAtIndexPath: and moveRowAtIndexPath:toIndexPath:.
Also take a look at tutorial that show how it is possible to implement.
Along with tableview delegates, use moveRow(at: <IndexPath>, to: <IndexPath>), to move your row programatically (automatically), without user interaction.
tblTabel.moveRow(at: <IndexPath>, to: <IndexPath>)
// Tableview Delegates
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// return boolean value for a specific or all cells, you want to enable movement
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// Handle array element movement along with your row/cell
}
I've been using editActionsForRowAt to create the built-in swipe left functionality to reveal quick-access buttons, like this:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
print("Is this working?")
//Code to create UITableViewRowAction buttons
return [button1, button2]
}
func tableView (_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
//I read I need this, so I have it
return true
}
func tableView (_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
//I read I need this, so I have it
}
Now my whole table, including the swipe, works great. Until...
My editActionsForRowAt function has code where if the user swipes a cell that's expanded (by default they're 50, but users can tap to expand the height) then editActionsForRowAt returns [], an empty array. I do that because I don't want really tall cells showing stretched out buttons.
After that, even after closing, swiping a cell (any cell) doesn't even call editActionsForRowAt. I checked by adding a print-line right at the beginning of that function and it doesn't print no matter what I do after swiping an expanded cell and returning [].
Any ideas on how to fix this?
Instead of returning an empty array from editActionsForRowAt, you should return false from canEditRowAt when a given row isn't editable.
When I tap on the cells of my table view, they darken to a grey color, and don't turn back to white until I tap on a different cell. Is there some sort of Boolean I have to set for it to not do that?
Here's a screenshot explaining my problem:
Links to other websites would be helpful, if it would mean a more detailed description. (Unless it's a super simple fix, then the right code or steps-to-take would be easier than a link.)
This is the default behaviour of UITableView.
You must call deselectRowAtIndexPath inside of didSelectRowAtIndexPath inside your UITableViewController class.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
Check out the iOS Documentation for more information.
UITableView
UITableViewDelegate
Swift 3
Option 1: (Which I always use)
To give it fade out animation after selected with gray you can do this:
func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
Option 2:
To remove the highlight effect completely you can add this line to your cellForRowAt :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = .....
cell.selectionStyle = .none
return cell
}
You can do this a couple ways...
tableView.allowsSelection = false
You can set the tableView in xCode Storyboard to not have any selection under the fourth tab.
Or, you can do this on the cell cell.selectionStyle = UITableViewCellSelectionStyle.None
What you want is ultimately going to be about what behavior you are going after. Just do a little experimenting.
Swift 3
In a custom cell add this:
override func awakeFromNib() {
super.awakeFromNib()
selectionStyle = .none
}
This ensures you won't even see the gray when the cell is tapped. This code in the UITableViewDelegate only deselects when tapped.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
Just Simply click on the cell and go to attributes inspector you will find Selection Style , select none.
You can change style by:
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
Swift 4.1
tableView.deselectRow(at: indexPath, animated: true)
In the storyboard or xib,
In the Tableview cell,
you can select selection to "none",
in the atributes inspector