Uitableviewcell Selected Background Color on Multiple Selection

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

cell.selectionStyle = UITableViewCellSelectionStyleGray;

UITableViewCell Selected Background Color on DidSelectRow

Do not change the backgroundColor of the contentView or of the cell itself, use the selectedBackgroundView instead:

 cell.backgroundView = UIView()
cell.backgroundView?.backgroundColor = // your color when not selected

cell.selectedBackgroundView = UIView()
cell.selectedBackgroundView?.backgroundColor = // your color when selected

After this initial setup, there is no need to change anything. Everything will work automatically.

Do not set cell.selectionStyle = .none because that will disable selection completely.

UITableView change color of multiple selection checkmarks

Note that in iOS 7, using

[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];

will not work as expected, because in iOS 7 this is now gray, even if you pass the constant above. See:

https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/c/tdef/UITableViewCellSelectionStyle

So use this to change color in the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method.

UIView *cellBg = [[UIView alloc] init];
cellBg.backgroundColor = [UIColor colorWithRed:(76.0/255.0) green:(161.0/255.0) blue:(255.0/255.0) alpha:1.0]; // this RGB value for blue color
cellBg.layer.masksToBounds = YES;
Cell.selectedBackgroundView = cellBg;

swift - How to set selection color when Multiple Selection is set to true in UITableVIew

You can do this with following code :
It works for me

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell : KichenListTableViewCell = tableView.cellForRow(at: indexPath) as? KichenListTableViewCell else {
return
}
cell.backgroundColor = UIColor.red
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
guard let cell : KichenListTableViewCell = tableView.cellForRow(at: indexPath) as? KichenListTableViewCell else {
return
}
cell.backgroundColor = UIColor.white
}

Hope it will helps you.Thank you

Background colours changing automatically in Multiple Cell Selection Swift

You should handle background color in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) .
Check and use below code. Hope it will work

 var selectedCells = Array<NSInteger>()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return sportsArr!.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath) as! FollowSportsCell

cell.sportL.text = sportsArr![indexPath.row]["title"] as? String

cell.selectionStyle = UITableViewCell.SelectionStyle.none

if self.selectedCells.contains(indexPath.row) {
cell.sportL.textColor = .white

cell.backImg.backgroundColor = UIColor(hexString: "4293CC")

if self.selectedCells.count > 1
{
collectionB[0].backgroundColor = UIColor(hexString: "4293CC")

collectionB[0].isEnabled = true
}
}
else
{
//Here Set your default color for cell backImgr background color
cell.sportL.textColor = // set default color

cell.backImg.backgroundColor = // set default color

}
return cell

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let cell = tableView.cellForRow(at: indexPath) as! FollowSportsCell

if self.selectedCells.contains(indexPath.row) {
//Here you will get selected cell and going to deselect...Do anything with deselection

if let index = self.selectedCells.index{$0 == indexPath.row}
{
self.selectedCells.remove(at: index)
}

cell.sportL.textColor = .black

cell.backImg.backgroundColor = UIColor(hexString: "E6E6E6")

selectedCell -= 1

selectedSports = selectedSports.filter{$0 != sportsArr![indexPath.row]["id"] as! Int}

if self.selectedCells.count < 2
{
collectionB[0].backgroundColor = .lightGray

collectionB[0].isEnabled = false
}

}
else
{
self.selectedCells.append(indexPath.row)
print(cell.sportL.text!)

selectedCell += 1

let sport = sportsArr![indexPath.row]["id"]

selectedSports.append(sport! as! Int)
}

self.yourtblView.reloadData()

}


Related Topics



Leave a reply



Submit