我试图找出如何获取数组中数组的计数(不知道技术术语)。
我正在创建UIViews组,我希望这些组的宽度根据组内UIView的数量动态变化。
我知道如何执行此操作(可能)的唯一方法是查找blockTitles
(组)的计数,但我在我的palletViewConstraint()
函数中提取此计数时遇到问题。< / p>
寻求有关此解决方案的帮助或建议以获得更好的解决方案。
干杯!
func mainViews(inputView: UIView){
<...>
//POPULATING PALLET WITH BLOCKS
let blockTitles1 = ["1", "2", "3", "4", "5"]
let blockTitles2 = ["6", "7", "8", "9", "10"]
let blockTitles3 = ["11", "12", "13", "14", "15"]
let blockTitles4 = ["16", "17", "18", "19", "20"]
let blockTitles5 = ["21", "22", "23", "24", "25"]
var group1 = createBlockGroup(blockTitles1)
var group2 = createBlockGroup(blockTitles2)
var group3 = createBlockGroup(blockTitles3)
var group4 = createBlockGroup(blockTitles4)
var group5 = createBlockGroup(blockTitles5)
palletView.addSubview(group1)
palletView.addSubview(group2)
palletView.addSubview(group3)
palletView.addSubview(group4)
palletView.addSubview(group5)
palletViewConstraint(palletView, groups: [group1, group2, group3, group4, group5], blockTitles: [blockTitles1, blockTitles2, blockTitles3, blockTitles4, blockTitles5])
}
func createBlock(title: String) -> UIView{
let block = UIView()
block.backgroundColor = UIColor.lightGrayColor()
block.translatesAutoresizingMaskIntoConstraints = false
let blockLabel = UILabel()
blockLabel.frame = CGRectMake(0, 0, 100, 100)
blockLabel.text = title
blockLabel.textColor = UIColor.darkGrayColor()
blockLabel.textAlignment = NSTextAlignment.Center
block.addSubview(blockLabel)
return block
}
func createBlockGroup(blockTitles: [String]) -> UIView {
var blocks = [UIView]()
let blockGroups = UIView()
blockGroups.frame = CGRectMake(0, 0, 100, 100)
blockGroups.backgroundColor = UIColor.redColor()
for blockTitle in blockTitles{
let block = createBlock(blockTitle)
blocks.append(block)
blockGroups.addSubview(block)
}
blockConstraint(blocks, mainView: blockGroups)
return blockGroups
}
func blockConstraint(blocks: [UIView], mainView: UIView){
<...>
}
func palletViewConstraint(inputView: UIView, groups: [UIView], blockTitles: [[String]]){
print(blockTitles.count)
}
func mainViewConstraints(inputView: UIView, pallet: UIScrollView, canvas: UIView){
<...>
}
}
答案 0 :(得分:0)
您可以使用
访问数组中的数组计数mainArr[0].count
这将得到mainArr中第一个数组的计数。
我建议继承UIView
并使用便利初始化程序将框架设置为除以视图数量(可以通过委托模式访问)的值。
以下是我设置的UIView
子类的示例,用于动态调整子视图网格。整个项目可以在我的github页面上找到:https://github.com/cmako10/UI-View-Groups
这是子类:
class GroupView: UIView {
var rows: Int!
var columns: Int!
var size: CGSize!
var origin: CGPoint!
enum TypeToAdd: String {
case row = "Row"
case column = "Column"
}
enum AlterAction: String {
case add = "Add"
case delete = "Delete"
}
convenience init(origin: CGPoint, size: CGSize, columns: Int, rows: Int) {
self.init(frame: CGRect(origin: origin, size: size))
self.size = size
self.origin = origin
self.rows = rows
self.columns = columns
}
override func drawRect(rect: CGRect) {
frame.size.width = size.width
frame.size.height = size.height
super.drawRect(rect)
for view in self.subviews {
view.removeFromSuperview()
}
let width = size.width / CGFloat(columns)
let height = size.height / CGFloat(rows)
let subviewSize = CGSize(width: width, height: height)
for y in 0..<rows {
for x in 0..<columns {
let newView = UIView(frame: CGRect(origin: CGPoint(x: (CGFloat(x) * width), y: (CGFloat(y) * height)), size: subviewSize))
let label = UILabel(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: subviewSize))
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center
label.font = UIFont.systemFontOfSize(15, weight: 10.0)
label.textColor = UIColor.blackColor()
label.center = CGPoint(x: newView.frame.midX, y: newView.frame.midY)
label.text = "[\(x), \(y)]"
newView.addSubview(label)
if y % 2 == 0 {
if x % 2 == 0 {
newView.backgroundColor = UIColor.lightGrayColor()
} else {
newView.backgroundColor = UIColor.blueColor()
}
} else {
if x % 2 == 0 {
newView.backgroundColor = UIColor.blueColor()
} else {
newView.backgroundColor = UIColor.lightGrayColor()
}
}
addSubview(newView)
}
}
}
func alter(type: TypeToAdd, action: AlterAction){
switch type {
case .column:
switch action {
case .add:
columns = columns + 1
case .delete:
columns = columns - 1
}
case .row:
switch action {
case .add:
rows = rows + 1
case .delete:
rows = rows - 1
}
}
setNeedsDisplay()
}
}
您应该下载并检查Xcode项目。动态更新UIView
的一般想法是删除所有子视图,然后在drawRect
中重新添加子视图,然后在需要更新{{1}的内容时调用setNeedDisplay
}}
修改强>
要访问GroupView
子阵列的计数,我会看到两种可能的解决方案。
blockTitles
之后,并引用{{1}根据需要不变。 palletViewConstraint
的开头定义一个变量来保存计数值(count
s的数组),使用高级函数Int
将所有的blockTitle计数附加到先前定义的变量,使用for循环和定义的count数组来运行palletViewConstraint
的其余部分。<强>示例:强>
示例1:
map
示例2:
palletViewConstraint
如果您需要详细说明,请询问!