gpt4 book ai didi

ios - 如何将 stackViews 添加到 UITableViewCell?

转载 作者:行者123 更新时间:2023-11-28 06:26:30 25 4
gpt4 key购买 nike

我正在尝试在 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 的函数,它的排列 subview 是按钮数组,还有一个生成 UIStackViews 数组的函数,其中包含带有按钮的 stackViews

    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 加载到单独的 UITableViewCell 中

    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

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

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

最佳答案

swift 中的类是按引用传递而不是按值传递。 UIButton 是一个类。目前您编写代码的方式只是创建按钮“a”、按钮“b”和按钮“c”的一个实例。但是这些实例已被添加到多个数组中,然后用于填充多个堆栈 View 。所以按钮“a”被添加到堆栈 View “x”,但随后被添加到堆栈 View “y”,当这种情况发生时,它从堆栈 View “x”中删除,因为 UIView 只能有一次一个 super View 。当它被添加到堆栈 View “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
}

关于ios - 如何将 stackViews 添加到 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41725960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com