Tableview selection hides my button within the tableview cell

▼魔方 西西 提交于 2019-12-07 13:46:58

问题


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.

In this i added button programmatically to the tableview cell.

Lets come to the problem.

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.


回答1:


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



回答2:


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




回答3:


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.




回答4:


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



回答5:


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



来源:https://stackoverflow.com/questions/30502189/tableview-selection-hides-my-button-within-the-tableview-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!