Change the section index background colour - swift

Currently I am using table.sectionIndexBackgroundColor = .clear which makes the section index semi-transparent, causing what you can see in screenshot below.
I am trying to remove the semi-transparent overlay, which ends above the home button area on iPhone X. Another option is to extend the section index background throughout this area and set a non-transparent colour, because this design on rounded display looks really weird.
Any ideas how to do this?
EDIT
The point is, if I change the background colour to any value, the area near home button remains semi-transparent while scrolling.

FIXED
func tableView(_ tableView: UITableView, cellForRowAt path: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "identifier", for: path)
let data = ...
cell.textLabel!.text = ...
// here it comes
cell.contentView.superview?.backgroundColor = UIColor.clear
return cell
}

Related

Xcode11, TableView Cell Selection Style doesn't work

I uploaded the screenshot, I am setting the (static)tableview cell selection style to blue, but it doesn't work when I run the simulator, it is still the grey color. Then I tried the code cell.selectionStyle, but it also does not work. Am I doing wrong? or understanding wrong on this issue? I tried the code using ".contentView.backgroundColor" to set the background when I select or de-select a cell, it works,....but why the "cell.selectionStyle" does not work? AS I understand, it is right the background color when I select a cell.
Below is what(code) I tried to set the selection style. My focus is also on the interface as the picture showed.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("I select it")
let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
selectedCell.selectionStyle = .blue
}
You can try this code
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.blue
cell.selectedBackgroundView = backgroundView

Change Button Background in UICollectionViewCell

I have a button inside a UICollectionViewCell (custom class) in a UICollectionView. When the button is clicked, it changes its background color. Works perfectly at first, but when I change the background color of the main view, I can't update the background color of the button. The background colors are stored in an array and here's the cell set-up function:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print("Rebuilding cells")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ColorCollectionViewCell
cell.button.tappedColor = backgroundColors[section]
cell.button.originalColor = backgroundColors[section + 1]
cell.button.delegate = self
cell.button.addTarget(self, action: #selector(didHitButton), for: .touchUpInside)
return cell
}
So if I start with section as 1, it loads the cell's button with the proper background color (black which is the first color in the section array. Then, I set the count to zero and grid.reloadData() which successfully wipes out the cells.
But then when I set section to 2 and reset the total cell count again, the cells don't adopt the new background color (section 2 color is green). They stay black from section 1.
How would I go about telling the UICollectionView to repaint the button background color in the cell when the data updates, where that background color comes from an array based on a variable for the index?
I think you should be using the .backgroundColor property of the views to set the background color to display it.
Other than that, with each tableView.reload() all your tableView delegate methods are called again and your cells are re-binded with the data that you provide... so you have to treat them like they're un-initialized. This could also point out to, that if you're not getting the right colors assigned, black might be their default color and maybe its not working for section 1 as well or you should check the data stored in your array (you can debug or print them).
For a cleaner flow of logic just do:
if indexPath.section == 0 {
// do this for section 0
} else if indexPath.section == 1 {
// do this for section 1
} else {
// for all other if any
}
Hope this helps you in someway...

Cannot Change Text of Label in TableView

I have a tableView which displays the address and phone number of a business. I want to use a label next to the address and phone number. For some reason, all of my labels say "Address" even though I have the phone label set as "Phone" in the storyboard and in the code. Why is it doing this?
How it looks in the storyboard
How it looks when I run it
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ProfileCell
cell.addressLabel.text = "Address:"
cell.phoneLabel.text = "Phone:"
cell.addressInfoLabel.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
cell.phoneInfoLabel.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
return cell
}
Remove following lines
cell.addressLabel.text = "Address:"
cell.phoneLabel.text = "Phone:"
My guess is you have set outlet reference to addressLabel, what should have been referred to phoneLabel. Check that.
I checked your code. The problem is with height of the labels. Address label is overlapping phone label. Here are the things you can do
Change the background colors of the address label and phone label. You will realise that address label is completely hiding phone label.
Remove aspect ratios for the labels and give some constant height.e.g. 30
Implement heightForRow for tableView and give some height e.g. 150.

UITableViewRowAction adjust dimensions (height)

so i have a table in which I have made a little "hack" so to say. The "hack" was to create a UIView with a color and a smaller height than the actual (now transparent) cell, to make it look like there was spacing between all the cells.
My issue is now, that there is a 10 units gap from the "imaginary" cell to the actual buttom of the cell. Meaning that when i add my UITableViewRowAction, it will look like this:
Obviously i want it to have the same height.
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let more = UITableViewRowAction(style: .normal, title: "More") { action, index in
print("more button tapped")
}
more.backgroundColor = UIColor.gray
return [more]
}
This is what i have now.. but really i have no clue as to how i would adjust the height
Edit I misread the question. What you really need to know is do you provide a custom view for the editing actions for your tableViewCell. There is a detailed tutorial for this here: https://www.raywenderlich.com/62435/make-swipeable-table-view-cell-actions-without-going-nuts-scroll-views

UITableView, Image in cell isn't showing, despite showing in UI View Hierachy inspector

I use a UITableView with custom UITableViewCells. In the cells I have a UIImageView. I assign an image to that image view from a url from my model. All this have been working fine until now.
When the cell is returned in cellForRowAtIndexPath, the image is fetched through a UIImageView extension and assigned to the image property when the async fetch returns.
The image doesn't show in the UIImageView, dispite having worked before (maybe this problem occured after Xcode 8 was released)
What's even more strange, it appear right in the UI View Hierachy Inspector in Xcode.
What's going on??
Screenshot from device:
Screenshot from UI View Hierachy Inspector in Xcode
Move your code that changes corner radius from awakeFromNib to layoutSubviews in your cell subclass like the example below. This should fix it.
override func layoutSubviews() {
super.layoutSubviews()
self.contentView.layoutIfNeeded()
imageView.layer.cornerRadius = CGRectGetHeight(imageView.bounds) / 2.0
imageView.clipsToBounds = true
}
You can also try changing it in your tableView class.
Like this:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier", for: indexPath) as! yourCellClassName //Change this to your cell
cell.imageView.layer.cornerRadius = 6
cell.imageView.clipsToBounds = true
return cell
}
Hope it will help you.