Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Uitableview Tableview选择在Tableview单元格中隐藏“我的按钮”_Uitableview_Swift_Selection_Custom Cell - Fatal编程技术网

Uitableview Tableview选择在Tableview单元格中隐藏“我的按钮”

Uitableview Tableview选择在Tableview单元格中隐藏“我的按钮”,uitableview,swift,selection,custom-cell,Uitableview,Swift,Selection,Custom Cell,还有一个逻辑相同的问题。我没有得到我想要的正确的解决方案。他们建议像那样对视图进行子类化。但是我需要单元格内的显示按钮 让我们来回答我的问题: 我有一个自定义的、通过编程创建的tableview 请看一下屏幕截图 在本文中,我以编程方式将按钮添加到tableview单元格中 让我们来讨论这个问题 当我选择任何单元格时,它也会隐藏按钮,我希望看到该按钮 我的代码在这里: func tableView(tableView: UITableView, cellForRowAtIndexP

还有一个逻辑相同的问题。我没有得到我想要的正确的解决方案。他们建议像那样对视图进行子类化。但是我需要单元格内的显示按钮

让我们来回答我的问题:

我有一个自定义的、通过编程创建的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时,它会更改单元格中子视图的背景色。在您的情况下,它会将红色按钮的颜色更改为与选择行相同的颜色(灰色)

您可以在此处看到解决方案:

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

从UITableViewCell文档中:

如果希望通过简单地添加其他视图来自定义单元格,则应将其添加到内容视图中,以便在单元格转换为编辑模式或从编辑模式转换为编辑模式时对其进行适当定位


阅读更多有关文档的信息时,它似乎会在高亮显示或选中时更改视图上的背景色。试试这个:


覆盖
setSelected(\uselected:Bool,动画:Bool)
setHighlighted(\uhighlighted:Bool,动画:Bool)
。在那里重新设置按钮的背景色,不要忘记调用
super

您可以添加自定义按钮:

class NoHighlightButton: UIButton {

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

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

 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
}
现在在DidSelectRowatinex方法中添加这个

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

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

cell.selectionStyle = .none

您可以提供单元格选择样式无。=>cell.selectionStyle=.None@HoaParis的不可能重复项是的,但我无法从中找到解决方案。如果您使用我的代码发布答案,这将很有帮助。swift中没有该功能。它们是,可能我只是复制粘贴了错误的内容:@Lydia当您对UITableViewCell进行子类化时就是这样。google for uitableviewcell子类化。。然后在子类中重写。我总是使用uitableview保持自己干燥(不要重复)。如果我将有几个部分显示一些uitableviewcell布局。。最好创建一个.nib(.xib)单元格和子类。如果你需要任何进一步的解释或例子。。让我知道注意**必须覆盖这两种方法**否则你会得到闪烁效果..真的谢谢.它起作用了.但是如果我在内容视图中添加按钮它不起作用,cell.contentView.addSubview(redBtn)它起作用了,我在selectedCell.contentView.subViews{}中对子视图做了这样的更改是的,如果要添加到内容视图,则在for循环中必须使用-contentView.subview