如何将 stackViews 添加到 UITableViewCell?

how to add stackViews to UITableViewCell?

我正在尝试在 UIStackViews 中动态创建 UIButtons,这些 UIButtons 将加载到 TableViewCell 中。

我有两个包含字符串值的数组:

array_1 = ["a" , "b" , "c"]
array_2 = ["x" , "y" , "z"]

我有一个创建 UIButton 的函数,以及一个生成一组 UIButton 数组的函数,该数组包含 'array_1' 中的一个值加上 'array_2' 中的所有值(即:[[a ,x,y,z], [b,x,y,z], [c,x,y,z]])

    func createButton(title : String) -> UIButton {
    let newButton = UIButton(type: .system)
    newButton.setTitle(title, for: .normal)
    newButton.backgroundColor = UIColor.lightGray
    return newButton
}

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

    for title in array_1 {
        topButtonArray += [createButton(title: title)]
    }
    for title in array_2 {
        buttomButtonArray += [createButton(title: title)]
    }

    for button in topButtonArray {
       finalButtonArray += [[button]+buttomButtonArray]
    }

   return finalButtonArray
}

现在我有一个函数可以创建一个 UIStackView,它的排列子视图是按钮数组,还有一个函数可以生成一个 UIStackView 数组,其中包含那些带有按钮的 stackView

    func createStackView(subViews : [UIButton]) -> UIStackView{
    let stackView = UIStackView(arrangedSubviews: subViews)
    stackView.axis = .vertical
    stackView.distribution = .fillEqually
    stackView.alignment = .fill
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.spacing = 5
    return stackView
}


    func generateStackViewArray() -> [UIStackView] {
    var stackViewArray = [UIStackView]()
    let finalButtonArray = generateButtons()
    for buttons in finalButtonArray{
        stackViewArray += [createStackView(subViews: buttons)]
    }
    return stackViewArray
}

最后,我想将这些 UIStackView 加载到单独的 UITableViewCells 中

    override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array_1.count
}

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

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let screenSize : CGRect = UIScreen.main.bounds
    let screenHeight = screenSize.height
    let relativeCellSizeDefault = screenHeight / CGFloat (array_1.count)

    return relativeCellSizeDefault
}

现在我的最终结果是一个 UITableView,有 3 个单元格(正如我想要的那样) 但在第一个和第二个单元格中,我只能看到第一个按钮(即:a/b),但在第三个单元格中,我得到了我想要发生的事情(即:a、x、y、z):

finalResultImage

我试着用我的逻辑找出问题,但我似乎找不到答案,为什么它只在最后一个单元格中加载整个按钮数组。

此外,我是初学者,所以可能会有一些冗余,或者我可能没有在某些事情上使用最佳实践 - 请随时纠正我,非常感谢您的帮助

类 in swift 是按引用传递而不是按值传递。 UIButton 是一个 class。目前您的代码编写方式仅创建按钮 "a"、按钮 "b" 和按钮 "c" 的一个实例。但是这些实例已被添加到多个数组中,然后用于填充多个堆栈视图。所以按钮 "a" 被添加到堆栈视图 "x",但随后被添加到堆栈视图 "y",当发生这种情况时,它从堆栈视图 "x" 中删除,因为 UIView 一次只能有一个超级视图。当它被添加到堆栈视图 "z" 时,会再次发生这种情况。您需要更改代码,以便为每个单元格的 "a"、"b" 和 "c" 按钮实例化新实例。类似于:

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

    for title in array_1
    {
        topButtonArray.append(createButton(title: title))
    }

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

        for title in array_2
        {
            buttonArray.append(createButton(title: title))
        }
        finalButtonArray.append(buttonArray)
    }

    return finalButtonArray
}