【问题标题】:how to to toggle visibility of arrangedSubviews in a UIStackView, thats inside a tableViewCell swift3如何在 UIStackView 中切换排列子视图的可见性,这在 tableViewCell swift3 中
【发布时间】:2017-01-24 20:22:52
【问题描述】:

我有一个表格视图,其中的单元格由 UIStackViews 填充,按钮为排列子视图,由几个函数创建。 stackView 中的顶视图是可见的,所有其他视图都是隐藏的,顶视图中的按钮有一个动作,当它被调用时,其他视图应该在可见和隐藏之间切换。

    func generateButtons() -> [[UIButton]]{
    var topButtonArray = [UIButton]()
    var finalButtonArray = [[UIButton]]()

    for title in array1 {
        topButtonArray.append(createButton(title: title , action: "buttonPressed"))
    }

    for button in topButtonArray {
        var buttonArray = [UIButton]()

        buttonArray.append(button)

        for title in array2 {
            buttonArray.append(createButton(title: title, action: "moveOn"))
        }
        finalButtonArray.append(buttonArray)
    }
   return finalButtonArray
}


func generateStackViews() -> [UIStackView] {
        stackViewArray = [UIStackView]()
        let finalButtonArray = generateButtons()

        for buttons in finalButtonArray{
             stackViewArray.append(createStackView(subViews: buttons))

        }
       for stackView in stackViewArray{
          let views = stackView.arrangedSubviews
          let hiddenViews = views[1..<views.count]
          for views in hiddenViews{
            views.isHidden = true
        }
    }
    return stackViewArray
}

func buttonPressed(){
    //let stackViewArray = generateStackViews()
    for stackView in stackViewArray{
        let views = stackView.arrangedSubviews
        let hiddenViews = views[1..<views.count]
        for view in hiddenViews {
            if view.isHidden == true{showViews(view: view)} else{hideViews(view: view)}
        }

    }

}

func showViews(view : UIView){
    UIView.animate(withDuration: 0.3) {
        view.isHidden = false
    }

}


func hideViews(view : UIView) {
    UIView.animate(withDuration: 0.2) {
        view.isHidden = true
    }

}



override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "First")!
      let stackViewArray = generateStackViews()
       cell.contentView.addSubview(stackViewArray[indexPath.row])

    return cell
}

现在发生的事情是只有最后一个单元格中的隐藏视图在可见和隐藏之间切换(无论我点击哪个单元格) - 我想我需要实例化所有单元格上的切换但我想不出办法这样做。

另一个问题是我希望顶视图只打开其单元格中的隐藏视图,我认为我需要在“cellForRowAt indexPath”之外以某种方式使用 indexPath.row。

【问题讨论】:

    标签: ios swift uitableview swift3 uistackview


    【解决方案1】:

    如果您将大量此类逻辑移到 UITableViewCell 子类中,您会感谢您的理智。

    不是对你的 sn-p 的完全重写(暗示通过情节提要设置一些视图,但除了没有情节提要之外,在代码中没有大的区别,你还需要覆盖单元格的初始化并设置子视图),但这是您可以调查的起点:

    class StackViewCell: UITableViewCell {
    
        // these could be set up in code in the `init` method if 
        // you don't want to use storyboards
        @IBOutlet var stackView: UIStackView!
        @IBOutlet var toggleButton: UIButton!
    
        var optionButtons: [UIButton] = [] {
            didSet {
                for button in optionButtons {
                    button.isHidden = optionsAreHidden
                    stackView.addArrangedSubview(button)
                }
            }
        }
    
        // iterates over buttons to change hidden property based on `optionsAreHidden` property
        var optionsAreHidden: Bool = true {
            didSet {
                optionButtons.forEach({ $0.isHidden = optionsAreHidden })
            }
        }
    
    
        @IBAction func toggleButtonPressed(button: UIButton) {
            optionsAreHidden = !optionsAreHidden
        }
    
        // set up stackview and toggle button here if not setting up in storyboard
        //init?(coder aDecoder: NSCoder) { }
    
    }
    

    然后视图控制器变得简单了很多。我不清楚每个单元格的堆栈视图是否具有相同的选项按钮集,或者选项按钮是否基于它们所在的行以某种方式与上下文相关。

    如果它们都相同,我也会将 generateOptionsButtons() 逻辑移动到 StackViewCell 中(或者实际上,如果它们对于每个单元格都相同,我可能会在情节提要中设置它们)。

    class OptionsViewController: UITableViewController {
    
        func generateOptionsButtons() -> [UIButton] {
            // create and return buttons for a cell
            // potentially move this into `StackViewCell` too...
        }
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "StackViewCellIdentifier", for: indexPath)
    
            if let stackViewCell = cell as? StackViewCell {
                stackViewCell.optionButtons = generateOptionsButtons()
            }
    
            return cell
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      相关资源
      最近更新 更多