3

There is another question with same logic.UITableViewCell subview disappears when cell is selected i did not get correct solution which i want.They suggest to subclass view like that.But i need display button within the cell itself.

Lets come to my question:

I have a customized and programmatically created tableview.

Please take a look at the screenshots.

enter image description here

In this i added button programmatically to the tableview cell.

Lets come to the problem.

enter image description here

When i select any cell it hides the button also,I want to visible that button.

My code here :

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


    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

    // here is the redbutton

           var redBtn = UIButton()
       redBtn = UIButton(frame: CGRectMake(0, 0, 40, 40))
       redBtn.backgroundColor = UIColor.redColor()
        cell.addSubview(redBtn)

    //label text just added from an array

     cell.textLabel?.textAlignment = NSTextAlignment.Center
     cell.textLabel?.text = items[indexPath.row]

     return cell

}

If need : Tableview creation code:

       var tableView: UITableView  =   UITableView()

      override func viewDidLoad() {
       super.viewDidLoad()
    tableView.frame         =   CGRectMake(0, 50, self.view.frame.width,self.view.frame.height);
    tableView.delegate      =   self
    tableView.dataSource  =  self
tableView.estimatedRowHeight = 30
 tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

    self.view.addSubview(tableView)



}

Thanks In Advance.

Community
  • 1
  • 1
Lydia
  • 2,017
  • 2
  • 18
  • 34
  • You can provide cell selection style none. => cell.selectionStyle = .None – Amit89 May 28 '15 at 09:21
  • possible duplicate of [UITableViewCell subview disappears when cell is selected](http://stackoverflow.com/questions/6745919/uitableviewcell-subview-disappears-when-cell-is-selected) – Duyen-Hoa May 28 '15 at 09:27
  • @HoaParis yes but i cannot able to find an solution from that.It will helpful if you post an answer with using my code. – Lydia May 28 '15 at 10:08

6 Answers6

2

Here is a solution. Give a tag number to the button.

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


var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

// here is the redbutton

       var redBtn = UIButton()
   redBtn = UIButton(frame: CGRectMake(0, 0, 40, 40))
   redBtn.backgroundColor = UIColor.redColor()
     cell.contentView.addSubview(redBtn)
   redBtn.tag = 101
//label text just added from an array

 cell.textLabel?.textAlignment = NSTextAlignment.Center
 cell.textLabel?.text = items[indexPath.row]

 return cell
}

Now in didSelectRowAtIndex method add this.

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

    var selectedCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!

    for subViews in selectedCell.contentView.subviews {

        if subViews is UIButton && subViews.tag == 101 {
            let button = subViews as! UIButton
            selectedCell.bringSubviewToFront(button)
            button.backgroundColor = UIColor.redColor()
        }
    }
}
Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Amit89
  • 3,000
  • 1
  • 19
  • 27
  • Really thanks.it worked.But if i add button to content view it wont worked, cell.contentView.addSubview(redBtn) – Lydia May 28 '15 at 10:37
  • It works, i changed like this for subViews in selectedCell.contentView.subviews { } – Lydia May 28 '15 at 10:39
  • Yes, if you are adding to content view then in for loop you have to use- contentView.subviews – Amit89 May 28 '15 at 10:41
1

Because UITableView changes the background color of your subviews in the cell when you select it. In your case, it changes the color of the button in red to same color as the selection line (gray).

You can see the solution here: UITableViewCell subview disappears when cell is selected

Community
  • 1
  • 1
Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
1

Reading up a bit more on the documentation it seems like UITableViewCell changes the background color on views when it is highlighted or selected. Try this:

Override setSelected(_ selected: Bool, animated animated: Bool) and setHighlighted(_ highlighted: Bool, animated animated: Bool). Re-set your buttons background-color in there and don't forget to call super.

Maciej Swic
  • 11,139
  • 8
  • 52
  • 68
  • that function did not available in swift. – Lydia May 28 '15 at 10:16
  • They are, maybe i just copy-pasted them wrong. https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instm/UITableViewCell/setSelected:animated: – Maciej Swic May 28 '15 at 10:17
  • @Lydia it is when you are subclassing UITableViewCell . google for uitableviewcell subclassing.. then you override in the subclass . i always sublcass uitableview to maintain myself DRY (dont repeat yourself) . if im going to have several sections with kindaof a some uitableviewcell layout displaying.. its better to create one .nib (.xib) cell and subclass. if u need any further explanation or example.. let me know – lorenzo gonzalez Nov 02 '17 at 14:54
  • Attention ** must override both methods ** otherwise you will get the blinking effect.. – lorenzo gonzalez Nov 02 '17 at 15:00
1

You can add a custom button:

class NoHighlightButton: UIButton {

    override var backgroundColor: UIColor? {
        get {
            return super.backgroundColor
        }
        set {
            if newValue != UIColor.clearColor() {
                super.backgroundColor = newValue
            }
        }
    }
}
Bannings
  • 10,376
  • 7
  • 44
  • 54
0

You should never use cell.addSubview(aSubview). Instead you should use cell.contentView.addSubview(aSubview). Can't say this is 100% your issue but it could be.

From the UITableViewCell documentation:

If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instp/UITableViewCell/contentView

Maciej Swic
  • 11,139
  • 8
  • 52
  • 68
0

just add these code in your cellForRow:

cell.selectionStyle = .none
Dmitry S.
  • 1,544
  • 2
  • 13
  • 22