TableView DidSelectRowAt will change background colors of other cells when I scroll - swift

I have a TableView sometimes with enough cells allowing me to scroll through the table. At the same time I have set my didSelectRow to switch the background color of the cell. It seems that after selecting a few then scrolling down I find that more cells have been selected out of my control.
Here is my didSelectRowAt:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
packSizesTableView.cellForRow(at: indexPath)?.backgroundColor = UIColor.yellow
packList[indexPath.row].picked = true
pickedRows.append(indexPath)
Is there another way I should be selecting my table cells other than changing the color if the indexPath.row?

You can set cell color in cellForRowAt method as below:-
If packList[indexPath.row].picked {
yourCell.backgroundColor = UIColor.yellow
} else {
yourCell.backgroundColor = UIColor.red
}

Short answer: You are seeing the colors change on the wrong cells because cells get reused, and you need to set the color to the correct option when a cell is reused.
In the tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) method you can check your packList array to see if the cell being dequeued needs to be selected or not and set the color there. This will handle cases where a new cell is being created as well as cells that are being reused.
if packList[indexPath.row].picked {
cell.backgroundColor = UIColor.selectedColor
} else {
cell.backgroundColor = UIColor.defaultColor
}
You have several options for handling selecting/deselecting, so here is one option. In tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) you can simply do:
packList[indexPath.row].picked = !packList[indexPath.row].picked
tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.whateverYouWant)
You could also do just update your packList array (which is needed for cellForRow), and override the logic in your cell's func setSelected(_ selected: Bool, animated: Bool) method.

Related

Expandable table cell content overflowing from cell

I have an expandable tableview in my project as part of my tableviewcontroller. The issue is that when the table is not expanded, the rest of the cell is displayed anyway, overlapping with the next cell. The method I used to create expandable tables is from this tutorial: https://dev.to/lawgimenez/implementing-the-expandable-cell-in-ios-uitableview-f7j
I think they changed some things because this code was written in an older version of xcode and swift.
I have tried changing some properties of the cells and the tableview but nothing has worked so far.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableViewData[indexPath.row].opened = !tableViewData[indexPath.row].opened
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if tableViewData[indexPath.row].opened == true {
return 99.5
}
else {
return 40.5
}
}
I expected the cell in the tableview to cut off the custom cell at the exact height of the cell in the tableview but instead the cell is small when not expanded but all of the custom cell is diaplayed and overflows from assigned area for the cell.
You should use
tableView.reloadRows(at: [indexPath], with: .automatic)
instead of
tableView.reloadData()
Another thing, you should use
if tableViewData[indexPath.row].opened {
}
instead of
if tableViewData[indexPath.row].opened == true {
}

Swift4: How to change UITableView cell color

I have a UITable view where you can add items by pressing a button. I want its cells to be clear, I already made its background semitransparent, but once you click on the button that allows you to save the items in it, the first you save has a white background. Then, if you press the button other times, all of the cells, except for the last created, become semitransparent, but one continues to be white. This is my code, how could I make it completely clear?
extension ViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return number.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let textCell = tableView.dequeueReusableCell(withIdentifier: "WordCell", for: indexPath)
textCell.textLabel?.text = "\(indexPath.row + 1) \(number[indexPath.row])"
let semitransparentBlack = UIColor(rgb: 0x000000).withAlphaComponent(0.3)
textCell.layer.backgroundColor = semitransparentBlack.cgColor
textCell.textLabel?.layer.backgroundColor = semitransparentBlack.cgColor
textCell.textLabel?.textColor = UIColor.white
return textCell
}
}
You can set the color using textCell.backgroundColor = semitransparentBlack

Xcode static cells with dynamic backgroundColor

Is there a possibility to set a dynamic backgroundColor with static cells in a UITableView?
For example the user pressed a button in a UITableView and then all cells in the same view change to a specific color.
I already figured out how to set a backgroundColor when the view shows up:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.black
}
Now I want to achieve the same when the view has already loaded. Any suggestions?
You can access the currently visible cells (the ones that have passed over the willDisplay milestone) via the visibleCells property. You can iterate over this array and do whatever you need:
for cell in tableView.visibleCells {
cell.backgroundColor = UIColor.black
}
Alternatively you can call reloadData() on the tableView, this will result in all cells being re-created and re-displayed.
You can use a delegate method called DidSelect this method works when a user tap on a cell in the tableView or inside an element, and returns the index path of selected Cell you can use that to edit background color or anything.
The code should be something like this optional func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) inside this function you can get the cell using cellForRowAtIndexPath and pass it the selected path and cast it as the cell type you have and then edit it or do whatever.
The full code would look something like this
optional func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath){
let cell = tableView.cellForRow(at: indexPath)
//use the cell object and do what you want
}

Tableview creates five rows on one row?

I have a tableView with a custom cell that I have told should give me 5 cells in return of this custom cell. The question now is that I am running the app and getting five rows on one row. I have changed from default size of cell to custom but that is nearly the only thing I have done. So now I wonder what can create this kind of problem?
The code of my tableView looks like this.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "theoryCell") as! theoryTVC
return cell
}
So the problem is you are using custom cell with custom height but you are not setting height for row in table view method.
Try this:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 100.0 //Choose your custom row height
}

Swift TableView Prevent Button color change on selection

I am having a hard time preventing color change on my background color for my IBAction button when I select a row in my tableView. The background of my button is set to green, but when I tap the table view cell, it becomes gray. I have tried to change the color through different functions (tableView didSelectRowAtIndexPath etc..). Any ideas on how I would prevent the background of the button to change?
Thanks in advance!
I have faced same problem with button backgroundColor in TableView and solved it this way.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomCell
cell.button.backgroundColor = UIColor.greenColor()
}
If you want to disable color change on selection entirely, change the selectionStyle attribute in your custom cell:
selectionStyle = .none
I solved the problem with the code Nirav provided me above:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomCell
cell.button.backgroundColor = UIColor.green
}
However, I noticed that the button background color continued to turn gray when I tapped the row, and held the 'tap' without releasing. I solved this by adding the following code as well:
func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomCell
cell.exploreButton.backgroundColor = UIColor.green
}