Custom Uitableviewcell Selection Style

Custom UITableViewCell selection style?

You can do this as follows. Set your table cell's selection style to UITableViewCellSelectionStyleNone. This will remove the blue background highlighting. Then, to make the text label highlighting work the way you want, instead of using the default UITableViewCell class, create a subclass of UITableViewCell and override the default implementation of setHighlighted:animated with your own implementation that sets the label colors to however you want depending on the highlighted state.

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if (highlighted) {
self.textLabel.textColor = [UIColor whiteColor];
} else {
self.textLabel.textColor = [UIColor blackColor];
}
}

Custom UI TableViewCell selected backgroundcolor swift

You've the right method already in there: didSelectRowAtIndexPath. In that method you can call tableView.cellForRowAtIndexPath(indexPath) and get your cell. Than you can set the cell-background to your color:

 func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("Row \(indexPath.row) selected")
let cell:YourCustomCell = tableView.cellForRowAtIndexPath(indexPath) as YourCell
cell.backgroundColor = UIColor.redColor()
}

Or, a better way would be to check in your cellForRowAtIndexPath method, if a cell is selected:

if(cell.selected){
cell.backgroundColor = UIColor.redColor()
}else{
cell.backgroundColor = UIColor.clearColor()
}

UITableViewCell Selected Background Color on Multiple Selection

Swift 4.2

For multiple selections you need to set the UITableView property allowsMultipleSelection to true.

myTableView.allowsMultipleSelection = true

In case you subclassed the UITableViewCell, you override setSelected(_ selected: Bool, animated: Bool) method in your custom cell class.

 override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

if selected {
contentView.backgroundColor = UIColor.green
} else {
contentView.backgroundColor = UIColor.blue
}
}

UITableView Cell selected Color?

I think you were on the right track, but according to the class definition for selectedBackgroundView:

The default is nil for cells in plain-style tables (UITableViewStylePlain) and non-nil for section-group tables UITableViewStyleGrouped).

Therefore, if you're using a plain-style table, then you'll need to alloc-init a new UIView having your desired background colour and then assign it to selectedBackgroundView.

Alternatively, you could use:

cell.selectionStyle = UITableViewCellSelectionStyleGray;

if all you wanted was a gray background when the cell is selected. Hope this helps.

Why can't you set the cell.selectionStyle = UITableViewCellSelectionStyleBlue?

Your not going to like the answer then. As of iOS 7, setting

cell.selectionStyle = UITableViewCellselectionStyleBlue;

will now give you gray. Apple got rid of it in iOS 7, so now selectionStyleBlue no longer exists as blue.. it is gray.

Here is the quote from Apple's Documentation of the UITableViewCell Class Reference:

  • Blue

The cell has a default background color when selected.

In iOS 7, the selection color is no longer blue. Use UITableViewCellSelectionStyleDefault instead.

Available in iOS 2.0 and later.

Of course, as I'm sure you have found, you can create a UIView and set it as the background to simulate a blue selection.

Here is a link for that approach: UITableView cell blue highlight on selection

Edit:

Just to be clear. UITableViewCellSelectionStyleBlue makes the selection style gray.

There is no default method of UITableViewCell that makes selection style a blue color.

In order to make selection style any color other than the default gray, you have to create a custom UIView like in the link I provided.

How to change selected color for table view cell?

You can do either one of this -

  1. Change the selectionStyle property of your cell.

For example: If you change it to UITableViewCellSelectionStyleGray, it will be gray.

 cell.selectionStyle = UITableViewCellSelectionStyleGray;

  1. Change the selectedBackgroundView property.

    let bgColorView = UIView()
    bgColorView.backgroundColor = UIColor.red
    cell.selectedBackgroundView = bgColorView

swift 4 correction:

    cell.selectionStyle = UITableViewCellSelectionStyle.gray

iOS customize row selection style of a table view?

UITableViewCell has a property for the backgroundView as well as the selectedBackgroundView.

When the cell gets selected it will automatically switch from showing the backgroundView to instead show the selectedBackgroundView.

So create a new UIView and give it a green background color (the size of the view doesn't matter as the cell will resize the view to cover the entire background). Then set that green view as the selectedBackgroundView of your cell. Now then the cell gets selected your green view will show.

If you want to have a gradient for your selection then you would draw a gradient in your selection view and make sure it's stretches correctly.



Related Topics



Leave a reply



Submit