Tableview选择会在tableview单元格中隐藏我的按钮

问题描述 投票:2回答:5

还有另一个问题有相同的逻辑.UITableViewCell subview disappears when cell is selected我没有得到我想要的正确解决方案。他们建议像这样继承视图。但我需要在单元格内部显示按钮。

让我们来问我的问题:

我有一个自定义和以编程方式创建的tableview。

请看一下截图。

在这个我以编程方式添加按钮到tableview单元格。

让我们来解决问题。

当我选择任何单元格时它也隐藏了按钮,我想要看到那个按钮。

我的代码在这里:

     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

}

如果需要:Tableview创建代码:

       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)



}

提前致谢。

uitableview swift selection custom-cell
5个回答
2
投票

这是一个解决方案。为按钮指定标签号。

 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
}

现在在didSelectRowAt Index方法中添加这个。

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

1
投票

因为UITableView会在您选择它时更改单元格中子视图的背景颜色。在您的情况下,它将红色按钮的颜色更改为与选择线(灰色)相同的颜色。

你可以在这里看到解决方案:UITableViewCell subview disappears when cell is selected


1
投票

在文档中阅读更多内容,看起来UITableViewCell会在突出显示或选中时更改视图的背景颜色。试试这个:

覆盖setSelected(_ selected: Bool, animated animated: Bool)setHighlighted(_ highlighted: Bool, animated animated: Bool)。在那里重新设置按钮背景颜色,不要忘记调用super


1
投票

您可以添加自定义按钮:

class NoHighlightButton: UIButton {

    override var backgroundColor: UIColor? {
        get {
            return super.backgroundColor
        }
        set {
            if newValue != UIColor.clearColor() {
                super.backgroundColor = newValue
            }
        }
    }
}

0
投票

你永远不应该使用cell.addSubview(aSubview)。相反,你应该使用cell.contentView.addSubview(aSubview)。不能说这是100%你的问题但它可能是。

从UITableViewCell文档:

如果要通过简单地添加其他视图来自定义单元格,则应将它们添加到内容视图中,以便在单元格进入和退出编辑模式时将它们正确定位。

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

© www.soinside.com 2019 - 2024. All rights reserved.