UITableView - Only highlight individual cell on touch - ios

I have an UITableView that has multiple rows. The user is only allowed to select 1 row. I want to show the grey selection effect only while touching the row, not after the row is selected.
I set the selection effect to Grey in interface builder. This allows me to see the selection effect while touching, but it also shows the grey selection color after I made my selection. To fix this I tried the following code from https://stackoverflow.com/a/10402278/6414904:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = self.tableView.cellForRow(at: indexPath) as! AnswerTableViewCell
tableView.deselectRow(at: indexPath, animated: true)
}
This works partly. When I select a cell it only shows the Grey selection effect while touching the cell but after selecting a cell the grey selection effect is gone. This is what I want but with this code added it also allows the user to select multiple rows which I do not want. With this approach I also can't use the didDeselectRowAt method for other functionalities.
How do I create a selection effect which only shows during touch/hold but is removed after a selection while only allowing 1 row to be selected at all times?

For this specially one delegate method is available.
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
// do something here
}

Add cell.setHighlighted(false, animated:true) to your didSelectRow call.
If I'm not mistaken, didSelectRow is only called after you release the button.

Related

Releasing highlighted state for UITableViewCell after returning to the screen

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.

Swift: Disable touches on a UITableViewCell

I have a UITableView on top of a MKMapView. I want to have similar functionality to how Apple's Maps app works. Where you can move a table up and down but still be able to move the map that is behind it. Right now I have a blank cell at index 0 that is clear and I want to be able to disable all touches that that cell receives and allows the map behind it to move, but when the cell in index 1 is touched that cell can scroll up and the rest of the table is now on top of the map.
If there is a better way to solve this problem I am up to try your solution!
func tableView(_ tableView: UITableView, didSelectRowAt
indexPath: IndexPath){
//UITableViewCell *cell = something
if indexPath.row == 0 {
cell.selectionStyle = .none
cell.isUserInteractionEnabled = false
}
}
What this code does is it makes sure that the cell is not highlighted when the user taps it. And then the second line in the if is to make sure that tableView:didSelectRowAtIndexPath: is not called which will be the case if the line is left out, and even though selectionStyle is none!

UITableViewCell styling resetting when scrolled off and back onto view

I currently have a TableView in my project, which is set up to turn a cell green when it is pressed, and back to clear if it is pressed a second time. However, if I scroll down to the bottom of the table view, and scroll back up, all my cells have been reset to their default clear colour.
I'm not sure how to go about fixing this issue, as anything I can find referring to it is in Objective-C rather than Swift. Any help and advice as to how to go about this would be great, thanks.
Everytime a UITableViewCell goes out of the screen, any function that you've written in the tableViewController/ViewController runs again.
for example in cellForRowAtIndexPath if you have a cell.setUpCell() or something similar, it will rerun and reset your values to the original values.
if you have a
var name = testName in your MainVC
and you update something in your cell, you should change the name in your mainVc too.
Every time you scroll or call tableView.reloadData() UITableView cells will reload. So, every time you select UITableViewCell, add selected index (indexPath.row) to an array(ex: selectedIndexArray) in your didSelectRowAt indexPath: delegate. If the cell you selected is already selected one, then remove the cell from selectedIndexArray.
And in your cellForRowAt indexPath: manage the cells using selectedIndexArray.
var selectedIndexArray:[Int] = [] //to save selected tableViewCells
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let isSelected = false
for each in selectedIndexArray
{
if each == indexPath.row
{
isSelected = true
}
}
if isSelected == true
{
//set selected cell color
}
else
{
//set default cell color
}
}
You need to write the logic of adding and removing cell indexes in your didSelectRowAt indexPath:.

How to set the background color of selected row in tableview which is child tableview?

I have a tableview which is inside the a tableview cell.
After click the "JAVA" inside "test1", I click the "About" Row which don't have the tableview inside:
But you can see that, the "About" and "JAVA" have the background colour at the same time. This is the automatic feature of tableview in iOS, I don't know how to code to prevent this happen. What I want is that when I click the "Java" inside "test1", the background color of "About" would disappear.
You are going to need to keep track of which cell was selected if you only want one cell selected at a time from both table views.
The best way to do that is to create a delegate for the nested tableview. So when that cell is selected it can call back to the parent tableview and let it know to unselect anything that was selected. The main class will need to keep track of the index that was selected as if another one is chosen then it can reach into the cell with selected cell and deselect that.
A little tricky, are you sure you can't get the same results from just using one tableview and multiple sections?
Use this code it helps you
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
selectedCell.contentView.backgroundColor = UIColor.greyColor()
}
To deselect the selected row use this code
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
var cellToDeSelect:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
cellToDeSelect.contentView.backgroundColor = colorForCellUnselected
}

Button in (static) cell disappears when clicking on the cell itself instead of the button

i'm having a weird issue with the code on my (static) cells. In multiple cells I have a UIButton. When I click on it, it's all fine, but when I click on the area next to the button (still in the cell itself) it turns grey (color when selected cell) + the button also disappears!
I tried to change the background of the cell to white when pressed with the following code:
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let myBackView=UIView(frame:cell.frame)
myBackView.backgroundColor = UIColor.clearColor()
cell.selectedBackgroundView = myBackView
}
but the issue is still there.
This is when the static cell isn't pushed
This is when I pressed the cell (NOT the button within the cell)
This is a common issue when selecting UITableViewCells, see Why do all backgrounds disappear on UITableViewCell select?
If you don't intend the cell to be selected, implement this delegate method:
- (NSIndexPath * _Nullable)tableView:(UITableView * _Nonnull)tableView willSelectRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath
and return nil when indexPath is the one of your cell. This will disable selection for this cell.
If none of your cells is intended to be selected, it's even easier, just set your UITableView's allowsSelection to NO (this can also be done in Interface Builder).

Resources