When a tableview cell is selected in my app, it remains selected until a different cell is selected.
How can I make the highlighting of the tableview cell fade out after a second or two similar to Apple's Find My Friends app and the Find My iPhone app. When switching the distance from In Miles to In Kilometers you can see that the cell is highlighted and then the highlight fades out.
Surely you searched for an answer first?
Anyway...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
Related
In UITableView, when tapping and holding a cell and (without releasing this cell) switching to another screen in tab bar and returning to the initial one, the cell remains highlighted (not selected) and if tapping on another cell, the first one gets selected.
Upon tapping and holding the first cell, tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) is called for the first cell. So far it is how it should be.
Upon tapping on another cell (after returning to the screen), tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) is called for the first cell. And then all methods for selection for the first cell.
Is there a way to remove this behaviour? And upon returning to the screen have no highlighted cells.
cell.setHighlighted(false, animated: false) doesn't work. Yes, it removes highlighting, but selection methods are again called for the first cell.
Have you tried to implement shouldHighlightRowAt and return false?
From the docs for tableView(_:shouldHighlightRowAt:):
Asks the delegate if the specified row should be highlighted.
Does anybody know if this application is using UITableView and have a UITextView inside it? If so, i want to ask how this is done. I also was wondering how a single tap can present an datepicker. I've uploaded a picture for clarity of what i want to achieve.
It can be either a label or a text field. In terms of presenting a date picker, you would use the function didSelectRowAt like kid_x said. I did this on an app I'm building with the help of this site. First I declared var datePickerVisible = false in the ViewController Class
and using the picture as an example, I toggled the visibility of the Date Picker like so
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 1 {
tableView.beginUpdates()
datePickerVisible = !datePickerVisible
tableView.endUpdates()
}
tableView.deselectRow(at: indexPath, animated: true)
}
I am working on a ViewController with a TableView populated with dynamic cells from a prototype nib. I have run into a dead-end trying to recreate certain a cell-expanding animation.
My goal:
When a cell is selected, the selected cell should "expand" (grow in height to twice it's starting size) while at the same time creating space between the cell directly above and below the selected cell.
I've found an example of EXACTLY the asethetic I am looking for in the app "Things". Below are two screenshots showing the table before and and after a cell is selected:
Screenshot of table BEFORE expansion
Screenshot of table AFTER expansion
The best way I can describe the desired animation is the UITableView version of "the parting of the Red Sea". When a cell is selected, the surrounding cells give way lending the selected cell more room.. and the user's focus.
What I've tried:
I found Simon Lee's method answering a similar question and implemented it into my project. And although it animates the row-height change perfectly, it only pushes the adjacent cells on one side of the selected cell. (ie: if the cell at index 4 is selected, all the cells from index 5+ move down but those from index 0-3 stay static. Thereby not achieving the look I'm seeking.)
Using that method, the relevant sections of my code looked something like:
var cellHeight: CGFloat = 72
var selectedCell: IndexPath?
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath == selectedCell {
return cellHeight * 2
} else {
return cellHeight
}
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedCell = indexPath
tableView.beginUpdates()
tableView.endUpdates()
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
selectedCell = nil
tableView.beginUpdates()
tableView.endUpdates()
}
Because that didn't result in my desired animation, I tried insertRows:at:with: to insert 2 empty cells (1 above and 1 below the selected row) -- then deleteRows:at:with: to remove them upon deselecting the cell. This ultimately made for a better looking animation, and looked closer to the "Things" example I'm shooting for. However this made the table overly complicated because by adding and removing cells each time a row is selected, it would change the index of the other cells making it frustrating to predict which cells would have what index at any given time.
A possible solution idea?
After working on this for a couple of days the only other way I could think to accomplish what I want is to somehow scroll the table slightly at the same time that the selected cell's height it changed.. so that as the cell expands (moving the following cells downward) it would make the previous cells appear to move upward. I'm hesitant to try this because it feels like a hack, there should be a better way to accomplish this.
ANY help would be thoroughly appreciated! I've been pulling my hair out at an alarming rate. Thank you to anyone who can share their knowledge.
I have a UITableView that has multi selection enabled. I have been using the "selection" to actually change the height of the rows, showing extra detail when "selected". E.g.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return (self.tableView.indexPathsForSelectedRows?.contains(indexPath) ?? false) ? 200 : 92
}
This seems to work pretty well. Until I start doing any swipes actions. When I add some swipe actions, the swipe action seems to clear all of my selections. I actually wanted to deselect the one I was swiping, so it would shrink back down. But the clearing of all my selections doesn't seem to trigger any of the normal delegate callbacks. Even though I have allowsMultipleSelectionDuringEditing set to true.
Is there a way to do this? Should I skip (ab)using the selection state as a way to indicate whether the row is showing details with a different height or not? Or is there a way to use it in conjunction with the behavior of the swipes being done in "edit mode" and clearing all of my selections?
The best way is using NSArray to store indexPath of selected cells, and base on saved indexPath you can check and do anything you want. another bug may happened in your code is: What happened in the case user make scroll on tableview? Does cell will reuse and lose select state? New cell reuse the old cell with 200 height will has wrong height?
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