Xcode11, TableView Cell Selection Style doesn't work - swift

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

Related

Does iOS 14 have some breaking changes that disable vertical scroll for Collection View? [duplicate]

I have a search form that uses a tableview. After updating Xcode 12 today the UISwitch, UITextField, UISlider no longer work when nested inside a UITableViewCell. Is there a property that has changed that I need to set to make this work again?
To be sure it wasn't just my project, I created a new project and nestled a UITextField inside of it and it doesn't work either.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let textField = UITextField(frame: CGRect(x: 5, y: 5, width: 400.0, height: 25.0))
textField.delegate = self
textField.backgroundColor = .blue
cell.addSubview(textField)
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("this will get called even when selecting the UITextField")
}
func textFieldDidBeginEditing(_ textField: UITextField) {
print("this is never called")
}
Your code was always wrong:
cell.addSubview(textField)
You must never add a subview to a cell. Add the subview to the cell's contentView.
The same happened to me since I upgraded to iOS 14.
This has worked for me when I add the subViews directly to the cell,
cell.contentView.isUserInteractionEnabled = true
Had similar issue, and been going on around for it... what was issue with my code is that under UITableViewCell I was doing this:
didSet {
if contentView.backgroundColor == backgroundColor { return }
contentView.backgroundColor = backgroundColor
for v in otherView.subview { v.backgroundColor = backgroundColor }
}
Removing this line here contentView.backgroundColor = backgroundColor did the trick. Cell is now visible and there is no duplicated contentView
Maybe this will help someone, since I found only answers regarding adding subviews directly to cell instead to cell.contentView
EDIT 1:
Okay, just wanted to update you on situation, issue was that my subviews where of type UIStackView and I had used subview where I actually should have used arrangedSubviews
Hope this will help someone

Change the section index background colour

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
}

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.

iOS8 Swift - Problems dynamically resizing tableview cells

Here's what my application looks like currently.
You'll see that when the subtitle text label is only one line is resizes correctly, but when there's multiple lines it gets all messed up. I think this has to do with the constraints possibly? Right now I'm using the auto-layout constraints. You can see them in the screenshot. Here's the code that creates my cells.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> DealCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Deal Cell", forIndexPath: indexPath) as DealCell
let currentBar = bars[indexPath.row] as BarAnnotation
cell.barName.text = currentBar.name
cell.deal.text = currentBar.deal
cell.distanceToBar.text = String(format: "%.3f mi", currentBar.distance)
// Set the height of the table view cells
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;
return cell
}
Any ideas?
Use tableView:heightForRowAtIndexPath: function in the UITableViewDelegate.
Using this you can individually set the height of each row separately.
For more information : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:
Or you can use auto layout settings like in this article.
http://useyourloaf.com/blog/2014/02/14/table-view-cells-with-varying-row-heights.html

Swipe to Delete in UITableViewCell has white background, need clear

I am trying to change the background color of the view that appears when you swipe a UITableViewCell row, the background color behind the 'Delete' button.
I tried changing the cell.editingAccessoryView but that didn't do anything.
UIView* myBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
myBackgroundView.backgroundColor = [UIColor clearColor];
cell.editingAccessoryView = myBackgroundView;
Any ideas?
I'm answering this because it took me a while to find an answer and this is the first entry that comes up in a search. By using the willDisplayCell method you can access the cells background color. Note that [UIColor clearColor]; will return white in so adjust your code accordingly.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor blackColor];
}
You're almost there. The UITableViewCell class has a backgroundView property, which is nil by default. Just create a new UIView as you've done in your question, then assign that to the backgroundView property of your cell.
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.backgroundView.backgroundColor = [UIColor clearColor];
I think it depends on how you are adding content into your cell.
When i added content to the cell directly using [cell addSubView] or [cell.contentView addSubView] i was having the same problem.
My workaround to this was:
Create a view
Add all your content(labels, images etc;) to this view
Finally then add the view to your cell using [cell addSubView:tmpView]
And you do not need to tamper with the backgroundView property anymore. I have tried this and works perfectly!
While these answers are right I feel that for most cases it's easier just to set the cell's background color in interface builder. By that I mean the actual background color property of the cell, not it's content view. There's no reason to do it dynamically if it's always gonna be the color that the content view is anyway.
iOS8 adds new method to UITableViewDelegate:
optional func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
You can create customizable UITableViewRowAction which device your row actions.
For example:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .destructive, title: "Cancel", handler: { action, indexPath in
// DELETE ROW HERE
})
deleteAction.background = .green // you can change background color
return [deleteAction]
}
For more check here and here
Nice example of usage new API: here
right way, just to let everyone know:
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
for subview in tableView.subviews {
if NSStringFromClass(type(of: subview)) == "UISwipeActionPullView", let button = subview.subviews.first, NSStringFromClass(type(of: button)) == "UISwipeActionStandardButton"
{
button.backgroundColor = .clear
button.subviews.first?.backgroundColor = .clear
}
}
}