3

I am trying to remove the highlight when selecting a table view cell, but want to keep the checkmark that appears.

When I try this in cellForRowAtIndexPath:

    cell.backgroundView = UIView()
    cell.backgroundView?.backgroundColor = UIColor.clearColor()

It only removes the highlight underneath the checkmark, rather than the whole row (refer to image attached).

enter image description here

When I try this in cellForRowAtIndexPath:

    cell.selectionStyle = UITableViewCellSelectionStyle.None

It no longer displays the checkmark.

UPDATE: tried this, does the same thing as cell.selectionStyle where it no longer does checkmark

func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return false
}

What is a good way of doing this? I want it to still function like a checkbox but just don't want the blue highlighting to occur. TableView is dynamically generated:

    checkBoxView = UITableView()
    checkBoxView.frame = CGRect(x: qView.bounds.midX, y: qView.bounds.midY, width: qView.bounds.width - 100, height: qView.bounds.height/1.5)
    checkBoxView.center = CGPointMake(qView.bounds.midX, qView.bounds.midY)
    checkBoxView.delegate = self
    checkBoxView.dataSource = self
    checkBoxView.tag = 100
    checkBoxView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    checkBoxView.setEditing(true, animated: true)
    self.qView.addSubview(checkBoxView)

Table View Functions:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.checkBoxContent.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = checkBoxView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
    cell.textLabel?.text = self.checkBoxContent[indexPath.row]
    cell.backgroundColor = UIColor.clearColor()
    cell.tintColor = UIColor.greenColor()
    return cell
}
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    return UITableViewCellEditingStyle(rawValue: 3)!
}
haitham
  • 3,398
  • 6
  • 21
  • 35

4 Answers4

5

In order to keep the checkmark, you can't set false to this shouldHighlightRowAtIndexPath method. If you do so, this wouldn't even show the checkmark on the left hand side at all.

What i have done is changing the "selectedBackgroundView" of cell which would keep the left-hand side checkmark and giving me the chance to set the background color. I enclose some code here and hope it would help.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! RecordTableViewCell
cell.selectedBackgroundView = UIView(frame: cell.frame)
cell.selectedBackgroundView?.backgroundColor = UIColor.orangeColor()

return cell
}
Hiren
  • 12,720
  • 7
  • 52
  • 72
user3431933
  • 103
  • 1
  • 9
2

None of the suggestions above worked for me. In Swift 5, add this line to your cellForRowAtIndexPath function:

cell.selectionStyle = UITableViewCell.SelectionStyle.none
Reefwing
  • 2,242
  • 1
  • 22
  • 23
0

Swift 1.2 use:

tableView.deselectRowAtIndexPath(indexPath, animated: true)

In the didSelectRowAtIndexPath delegate method.

cheborneck
  • 61
  • 5
-1

Theres an official way to do this with a UITableView, which is to use this :

   optional func tableView(_ tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool

If you return YES for this method, then the tableview will highlight a cell when it is clicked.

Also note if you dont want to use that, that you need to change the contentView.backgroundColor, rather than just the cell backgroundColor. But the highlighting route is the best one to go down.

Documentation here : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:shouldHighlightRowAtIndexPath:

Luke Smith
  • 1,218
  • 12
  • 17
  • just tried this. Functioned the same as changing cell.selectionStyle property where it no longer selects the cell (it doesn't show the checkmark and it doesn't call the didSelectRow method – haitham Jul 13 '15 at 18:19
  • also changing cell.contentView property didn't do anything – haitham Jul 13 '15 at 18:20
  • Dont return false here - return YES as I said. Also try changing the selectionStyle property of the cell - there are many options here for the appearance of the cell when selected - eg UITableViewCellSelectionStyleGray will make the cell grey when selected. – Luke Smith Jul 13 '15 at 19:01
  • tried this: cell.selectionStyle = UITableViewCellSelectionStyle.Gray but it still selected as blue. – haitham Jul 13 '15 at 19:26