Adding cells to visible empty UITableView show up with white background - ios

Using Xcode 10+, Swift 4+, iOS 11+
I've created a simple Search View that has a SearchBar at the top,
with a UITableView beneath.
When the view is first displayed, the Table is empty, and as the user enters text into the search bar, I add results to the table. Functionally, everything works ok.
Next, I've made the main View background blue and want the entire TableView to be clear so I can see the background.
I've set the table view background in the "viewDidLoad" :
myTable.backgroundColor = UIColor.clear
In "cellForRowAt" I set the cell background to clear:
var cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "cell")
cell.backgroundColor = UIColor.clear
cell.contentView.backgroundColor = UIColor.clear
However, when the search returns values and cells are added to the table,
they always have a white background instead of clear.
If I pop and then push the Search View without clearing the table, the existing table cells show up (redraw) the way I want them - clear.
I've also tried adding this to "didFinishLaunchingWithOptions" but it does not help:
UITableViewCell.appearance().backgroundColor = UIColor.clear
How can I make the cells show clear when they are first created?

try to set cell or tableView backgroundColor when cell will dispaly.
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.clear
}
or you can override function setBackgroundView to debug problem.

Related

two UITableView in a UICollectionView using the same reference?

I have a UICollectionView with 4 views in it. Each of these views have a UITableView inside it with custom cells. Each cell of the UITableView has a UIButton inside it and I have 2 cells per UITableView.
Something strange is happening. I have an action function for each button so that when a button is clicked, it becomes purple. The strange thing is this: if I scroll to the 4th view of my collection view and click on a button, it becomes purple as expected but then when I scroll to the 1st view of my collection view, the same button that I clicked in the 4th view (either the first one or second one) is also purple as if the 4th view of my collection view was referencing the items of my 1st view of the collection view.
I don't know at which point the 1st view becomes the same as the 4th view but here is a sample of the code:
// this is the cellForItemAt of my UICollectionView, very basic
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellView = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PollCellView
return cellView
}
// THIS IS ANOTHER FILE HERE
// this is part of my view that populate the UICollectionViews
class PollCellView: UICollectionViewCell {
// the table view
let questAndAnswersTableView : UITableView = {
let tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.separatorStyle = .none
tableView.allowsSelection = false
return tableView
}()
// I add the tableview in the view here
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(questAndAnswersTableView)
// a classic cellForRowAt of my UITableView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "AnswerCell2", for: indexPath) as! AnswerCell2
return cell
}
// THIS IS ANOTHER FILE HERE
// this part is my custom cell of the UITableView
class AnswerCell2: UITableViewCell {
let answerTextButton: UIButton = {
let answerButton = UIButton()
answerButton.setTitle("initial text", for: .normal)
return answerButton
}()
// I add the button to the cell here
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(answerTextButton)
// I define the action function
answerTextButton.addTarget(self, action: #selector(answerClicked), for: .touchUpInside)
}
// the action to make the button purple
#objc func answerClicked(sender: UIButton) {
sender.backgroundColor = UIColor(displayP3Red: 0.6902, green: 0.7176, blue: 0.9922, alpha: 1.0)
}
[EDIT FOLLOWING THE ANSWERS RECEIVED]
Dequeuing is definitely not as simple as it first seems... You can't trust that the tableview dequeued in a given collection view is really the one you expect... You need to keep track of the content (model) yourself. One thing that made it easier to fix is to use closures between the cell of the TableView and the cell of the UICollectionViewCell... You can very easily pass data from one to the other (like what indexPath was clicked, etc.).
when you're managing multiple nested tableviews or collectionviews or even when you're managing this yourself, you have to set up a system of arrays that when the button is clicked, you add those indexpaths to an array called "indexPathsThatArePuple" then when you after each button click, inside the button click function, you pass the indexPath and add that to the array. If the button is already inside the array when the button click function is pressed, then you remove that indexPath. in the button click function, after you add or remove indexPaths, you reload the collection view which will reload the tableviews and then inside "cellForItemAtIndexPath" you check the indexPathsThatArePuple array and then write "if indexpath is contained in indexPathsThatArePuple" then set the cell color to purple.
I understand what you're trying to do, but you don't realize or understand just how complicated the cell reuse system is in iOS. The first thing you need to do is wrap your mind around the idea the that you have to manually manage cell states due to cell resuse. The purple cell showing up in another table is from cell reuse. Apple won't automatically manage this for you, you have to do it yourself. As i described above and even my description above is not that cut and dry since you'll likely struggle with this for weeks until you grasp the concept. Good luck, you'll eventually get it.
It seems to be caused by UICollectionView cell reuse mechanism.
When the cell is created and scroll out of the screen, it will be reused later if a new cell with the same identifier is required. In your case, when you scroll down, the 1st view is reused as the 4th view, And when you scroll to the top, The 4th view is reused as the first view. If the method prepareForReuse happens to be not implemented, the UI status of the cell will not be change, so you will see the button with purple color.

Static table view cells: Clear color not corresponding

I have the below controller with the cell's, content view's, and table view's backgroundColor set to .clear, however, there is still a white background which I can't figure out what it is corresponding to.
It is due to Your table view cell colour.
Select table view cell:
Set its background colour as clear.
I suggest you to debug it in your Xcode. To do this you can launch it on you device(or on simulator), click 'Debug View Hierarchy' button.
Then by Mouse Click + Dragging on the screen rectangle you can rotate all layers of your screen and see which view is causing that white background.
this white background is due to table view cell select table view cell and from navigator make its background clear. or you can do it in
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellIdentifier", for: indexPath) as! YourCellClass
cell.backgroundColor = .clear
}
this will solve your problem...
:)

Background applied to the controller not getting applied to table view cells in storyboard swift

I have a storyboard consisting of a background image on top of which rests a table. My issue is that the background image gets applied to the table but not to the cells of the table, they still remain white.
What is it that I am doing wrong?
Please help
You need to set background color as clear color of tableview cell as well as it's content view.
You should do clear back ground color of your cell by below delegate method.
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell,
forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.clear
}
in cellForRowAtIndexpath method, use
cell.backgroundColor = UIColor.clear

UITableViewRow displays Buttons without Color

I a have 2 Buttons within my custom TableViewCell.
The Buttons are in bright colors. (BackgroundColor set)
However: Running the app - The Buttons color disappear and it becomes white:
I tried to programmatically change the color in viewDidLoad but Xcode doesn't react.
Any Ideas?
The heart of the problem is that you have introduced buttons but you are also setting the cell's textLabel!.text. You can't mix and match like that. If you're going to use a custom cell, you must use a completely custom cell.
Set the cell's type to Custom, drag a label into it, use a custom cell class, give it an outlet to the label, set this label's text, and all will be well.
Here's my custom cell with an outlet:
class MyCell : UITableViewCell {
#IBOutlet var label : UILabel!
}
Here's my cellForRow:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MyCell
cell.label.text = "Hi" // NOT cell.textLabel!.text
return cell
}
As you can see, the result is that button appears just fine.
You have an extra word in there:
cell.noButton.backgroundColor = UIColor.greenColor()

UIVisualEffectView and UITableView

I've put a UITableView into a UIVisualEffectView to achieve vibrancy. Everything works fine except for the bounce feature. The space where no cells are while scrolling gets drawn in some grey color instead of the blurred effect. I have tried to set all the background colours to clear but the blur still won't show. Is there any way to give the background the same effect as the rest of the table view?
You can find the demo project here
set Clear color of Table view, TableviewCell and ContainerView from Property. if not Clear then put code in your view Controller. also selection of cell none option.
SWIFT
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = UIColor.clearColor()
}
So I've found the answer. Instead of making the background a clear color, you need to set a white color.
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.backgroundColor = UIColor.whiteColor()
}

Resources