- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 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):
我试着用我的逻辑找出问题,但我似乎找不到答案,为什么它只在最后一个单元格中加载整个按钮数组。
另外,我是一个初学者,所以可能会有一些冗余,或者我可能没有在某些事情上使用最佳实践 - 请随时纠正我,非常感谢你的帮助
最佳答案
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/
我有一个带有 rowHeight 属性设置为 UITableViewAutomaticDimension 的 TableView 的 View Controller : class ViewContr
我在 IB 中创建了一个 Stackview,它具有垂直方向。此堆栈 View 与父 View 的宽度相等。 例如,现在我以编程方式创建了一个 Stackview let stackViewHoriz
我有一个 Storyboard,其中有一个用于 UIView 的单独 View Controller 。在 UIView 中,我必须在单行中加载 10 个按钮,而在 iPad 中,我必须在两行中加载
我有一个看起来像这样的布局。 View Controller 嵌入在导航 Controller 中,而导航 Controller 又嵌入在选项卡栏 Controller 中,因此 View 同时具有顶
在 UITableViewCell 中,我创建了一个垂直的 stackView。 我在垂直堆栈 View (由一个 UILabel(A) 和 UIImageView 组成)和一个 UILabel(B)
我使用 Xcode 10/Swift 5/iOS 12 并得到了这个布局: 红色(垂直)StackView 设置为: 分布:“平均填充” 高度:300(固定) 框架:10(每边) 间距:10 5 个水
我正在研究实现自定义控件 iOS 教程(我会链接它,但不允许使用两个以上的链接)。 我现在在 StackView 中生成了 5 个按钮。我为每个按钮实现了这些约束: button.translates
我正在尝试实现 UIView这将是一个标题。 备注 我只专注于在这个问题中实现标题(如下所示)。 这是它的外观(标题为黄色) 基本上,标题 UIView应该有 UIBUTTON一直到左边和一个UILa
我有一个 UIStackView这是根据其宽度改变轴。它包括两个UView仅限s。有一个非常简单的设置(可以复制/粘贴到默认的 Xcode 项目): class ViewController: UIV
我将三个垂直堆栈 View 放到一个水平堆栈 View 中。 虽然水平堆栈 View 的分布设置为等间距,但第二个垂直堆栈 View 在模拟器上左对齐(第一个左对齐,第三个右对齐)。 会不会有什么原因
是否可以在设备处于纵向模式时使用垂直堆栈 View ,但在设备处于横向模式时切换到水平堆栈 View ,以充分利用空间。 最佳答案 单击属性检查器中的小加号按钮添加一个变体,在此屏幕截图中我已经添加了
我有一个包含堆栈 View 的 ViewController。在此堆栈 View 中还有 3 个堆栈 View 。第一个包含 3 个标签,第二个包含更多带有按钮和标签的堆栈 View ,第三个包含 2
请告诉我我是否选择了正确的方法或者还有其他选择吗? 我从服务器收到一组对象。 根据任务,我必须将数组的每个对象放置在表格单元格中 我不知道会有多少对象,我只是说它们都有相同的结构,并且应该自动放置。
我在垂直 StackView 中有一个Horizontal StackView。 水平 StackView 包含图像(w=30,h=30)和标签。 需要 我需要根据标签固有内容的大小使图像和标签水
我有一个堆栈 View ,通过更改间距来折叠和展开堆栈 View ,将其用作下拉菜单。 堆栈 View 内有相同大小的按钮。当间距为负时,它们会出现在彼此的顶部。我设置图层 zIndex 来确定要在顶
我正在 Stackview 上工作并添加两个标签,我想调整标签的位置,使得下面的标签应该有点右侧。例如,在 UITableViewCell 中,我们有一个标题和第二个描述,随着描述部分内容的增加,一直
在自定义表格单元格中,我尝试在单击按钮时隐藏/显示 View 。我能够实现这一点,但无法删除空白。如何删除这些空格。我对堆栈 View 使用了自动约束,但似乎没有用。您能建议一种简单的方法来在 Vie
我有一个带有 UIStackView 的表格 View 单元。在我的 cellForRow 方法中,我根据某些因素将 stackview 的轴从水平更改为垂直。 由于某种原因,当我将包含表格单元格的
我想用不同的对齐方式对齐 3 个堆栈 1-Image 0-0 两边如下图 2- 用户信息 + 文字 10-10 形成两边 问题是当我将它们堆叠在一起时,我只能将它们全部设置为 0 到所有边距,每堆没有
我正在尝试在 TableView 的单元格方法中使用新的 StackView 来满足某些要求 一些背景 - 我有一个 TableView,它的单元格中有一个 stackView。 stackView
我是一名优秀的程序员,十分优秀!