Lydia
Lydia

Reputation: 2117

Tableview selection hides my button within the tableview cell

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.

Upvotes: 3

Views: 1727

Answers (6)

user16058303
user16058303

Reputation: 11

just add these code in your cellForRow:

cell.selectionStyle = .none

Upvotes: 0

Amit89
Amit89

Reputation: 3000

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()
        }
    }
}

Upvotes: 2

Bannings
Bannings

Reputation: 10479

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
            }
        }
    }
}

Upvotes: 1

Maciej Swic
Maciej Swic

Reputation: 11359

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.

Upvotes: 1

Maciej Swic
Maciej Swic

Reputation: 11359

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

Upvotes: 0

Duyen-Hoa
Duyen-Hoa

Reputation: 15784

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

Upvotes: 1

Related Questions