【问题标题】:Tableview selection hides my button within the tableview cell表格视图选择将我的按钮隐藏在表格视图单元格中
【发布时间】:2015-08-10 16:52:52
【问题描述】:

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

让我们来回答我的问题:

我有一个自定义和以编程方式创建的表格视图。

请看截图。

在此我以编程方式将按钮添加到 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


【解决方案1】:

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

您可以在此处查看解决方案: UITableViewCell subview disappears when cell is selected

【讨论】:

    【解决方案2】:

    你应该永远使用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

    【讨论】:

      【解决方案3】:

      阅读更多关于文档的内容,似乎UITableViewCell 在突出显示或选择视图时会更改视图的背景颜色。试试这个:

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

      【讨论】:

      • 该功能在 swift 中不可用。
      • 他们是,也许我只是复制粘贴错了。 developer.apple.com/library/ios/documentation/UIKit/Reference/…:
      • @Lydia 当您继承 UITableViewCell 时。 google for uitableviewcell 子类化..然后你在子类中覆盖。我总是 sublcas uitableview 以保持自己干燥(不要重复自己)。如果我将有几个部分显示一些 uitableviewcell 布局.. 最好创建一个 .nib (.xib) 单元格和子类。如果你需要任何进一步的解释或例子..让我知道
      • 注意**必须覆盖这两种方法**否则你会得到闪烁效果..
      【解决方案4】:

      您可以添加自定义按钮:

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

      【讨论】:

        【解决方案5】:

        这里有一个解决方案。给按钮一个标签号。

         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
        }
        

        现在在 didSelectRowAtIndex 方法中添加这个。

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

        【讨论】:

        • 真的很感谢。它工作了。但是如果我将按钮添加到内容视图它不会工作,cell.contentView.addSubview(redBtn)
        • 它有效,我为 selectedCell.contentView.subviews { } 中的子视图进行了这样的更改
        • 是的,如果您要添加到内容视图,那么在 for 循环中您必须使用 - contentView.subviews
        【解决方案6】:

        只需将这些代码添加到您的 cellForRow:

        cell.selectionStyle = .none
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-02-27
          • 2018-09-22
          • 1970-01-01
          • 1970-01-01
          • 2017-01-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多