Tableview Image Content Selection Color

tableview image content selection color

Try this:

    import Cocoa

class ViewController: NSViewController {
@IBOutlet weak var tableView:NSTableView!
var selectIndex = -1
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self

}

func tintedImage(_ image: NSImage, tint: NSColor) -> NSImage {
guard let tinted = image.copy() as? NSImage else { return image }
tinted.lockFocus()
tint.set()

let imageRect = NSRect(origin: NSZeroPoint, size: image.size)
NSRectFillUsingOperation(imageRect, .sourceAtop)

tinted.unlockFocus()
return tinted
}
}

extension ViewController:NSTableViewDataSource, NSTableViewDelegate{
func numberOfRows(in tableView: NSTableView) -> Int {
return 3
}

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView?{

let result = tableView.make(withIdentifier: "imageIcon", owner: self) as! NSTableCellView
if selectIndex == row{
result.imageView?.image = self.tintedImage(NSImage(named:"file")!, tint: NSColor.green)

}else{
result.imageView?.image = self.tintedImage(NSImage(named:"file")!, tint: NSColor.gray)

}
return result

}

func tableView(_ tableView: NSTableView, didAdd rowView: NSTableRowView, forRow row: Int) {
if selectIndex == row{
rowView.backgroundColor = NSColor.blue
}else{
rowView.backgroundColor = NSColor.clear
}
}

func tableViewSelectionDidChange(_ notification: Notification) {
let table = notification.object as! NSTableView
self.selectIndex = tableView.selectedRow

print(table.selectedRow);
table.reloadData()
}
}

Note: Change imageView tintColor color as per your requirement.

table row selection with color
Hope it will help you.

How to change color of UITableViewCell when selecting?

iOS 6.0 and later

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
// Add your Colour.
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
[self setCellColor:[UIColor whiteColor] ForCell:cell]; //highlight colour
}

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
// Reset Colour.
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
[self setCellColor:[UIColor colorWithWhite:0.961 alpha:1.000] ForCell:cell]; //normal color

}

- (void)setCellColor:(UIColor *)color ForCell:(UITableViewCell *)cell {
cell.contentView.backgroundColor = color;
cell.backgroundColor = color;
}

Custom UITableViewCell

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

UIView * selectedBackgroundView = [[UIView alloc] init];
[selectedBackgroundView setBackgroundColor:[UIColor colorFromHexString:@"5E6073"]]; // set color here
[self setSelectedBackgroundView:selectedBackgroundView];
}

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, if all you wanted was a gray background when the cell is selected, you could use this:

cell.selectionStyle = UITableViewCellSelectionStyleGray;

Change tableview row text and image color

Add this:

override public func setSelected(selected: Bool) {
super.setSelected(selected)
// Do your customization here

// changing text color
self.textLabel?.textColor = selected ? UIColor.blueColor() : UIColor.blackColor()
}

Change custom cell image color when selected in uitableview?

You should be changing the cell data inside your MenuTableViewCell custom class. There will be a methods in there that control the selected, highlighted state. The method will look something like this example,

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
//Change the text colour & image
} else {
//Change it back to whatever is was
}

}

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

Swift UITableview custom cell Imageview color change in another function

If you call tableView.dequeueReusableCell, that creates a new cell. You need to pass in the actual cell (or at least its IndexPath to be able to access the cell from the tableView.

You should break up your theming function into a separate one for table view cells, which you'll call from cellForRowAt and another one, which you'll call from viewWillAppear.

Some further improvements: you should always use an enum when you have a finite and immutable set of values that a certain thing can get a value from, specifically Theme in your case.

enum Theme {
case blue
case red
case green

var themeColor: UIColor {
switch self {
case .blue:
return .blue
case .red:
return .red
case .green:
return .green
}
}
}

class VC: UITableViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setGlobalTheme(to: .blue)
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
setTheme(for: cell, to: .blue)
return cell
}

func setGlobalTheme(to theme: Theme) {
navigationController?.navigationBar.tintColor = theme.themeColor
}

func setTheme(for cell: CustomCell, to theme: Theme) {
cell.Icon.image = UIImage(named: "pic")
cell.Icon.setImageColors(color: theme.themeColor)
}
}

If you want to be able to change the theme and then apply those changes to your cells, you either need to call tableView.reloadData after you changed the theme or even better, define below function, which iterates through all visible cells and updates them.

func changeTheme(to theme: Theme) {
setGlobalTheme(to: theme)
tableView.visibleCells.forEach {
guard let customCell = $0 as? CustomCell else { return }
setTheme(for: customCell, to: theme)
}
}

Just make sure to call changeTheme(to:) when you want to update the theme.

How to change the blue highlight color of a UITableViewCell?

You can change the highlight color in several ways.

  1. Change the selectionStyle property of your cell. If you change it to UITableViewCellSelectionStyleGray, it will be gray.

  2. Change the selectedBackgroundView property. Actually what creates the blue gradient is a view. You can create a view and draw what ever you like, and use the view as the background of your table view cells.

UITableView Static cells - Selection color not working

Add the following code to your TableView: cellForRowAtIndexPath method.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

// Write code for create your cell and properties regarding that cell
//add this line
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor blueColor];
[cell setSelectedBackgroundView:bgColorView];

return cell;
}

How do I set the background color of the editing accessory of a table view?

You have to do it in the tableView:willDisplayCell:forRowAtIndexPath: method. You can it like this:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "MyText"
return cell
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = UIColor(red: 0xca / 0xff, green: 1, blue: 0xc7 / 0xff, alpha: 1)
}

Edit:

Here is the visual result of the code above:
Sample Image



Related Topics



Leave a reply



Submit