Arrays 如何将这些单独的标签压缩到自定义UICollectionView单元格中的标签数组?
这段代码工作得非常好,但我假设有一种更有效的方法来做这样的事情。我将字符串数组的前四项(此列表可以包含无限量的变量)作为“快照”视图提供给用户。下面的代码位于自定义单元格类的内部Arrays 如何将这些单独的标签压缩到自定义UICollectionView单元格中的标签数组?,arrays,swift,uicollectionview,uicollectionviewcell,Arrays,Swift,Uicollectionview,Uicollectionviewcell,这段代码工作得非常好,但我假设有一种更有效的方法来做这样的事情。我将字符串数组的前四项(此列表可以包含无限量的变量)作为“快照”视图提供给用户。下面的代码位于自定义单元格类的内部 var tray: Tray? { didSet { guard let trayTitle = tray?.title else { return } guard let trayItems = tray?.items else { return } // t
var tray: Tray? {
didSet {
guard let trayTitle = tray?.title else { return }
guard let trayItems = tray?.items else { return }
// tray.items will give an array of strings
trayTitleLabel.text = trayTitle
if trayItems.count == 1 {
firstSnapshotLabel.text = trayItems[0]
secondSnapshotLabel.text = ""
thirdSnapshotLabel.text = ""
fourthSnapshotLabel.text = ""
} else if trayItems.count == 2 {
firstSnapshotLabel.text = trayItems[0]
secondSnapshotLabel.text = trayItems[1]
thirdSnapshotLabel.text = ""
fourthSnapshotLabel.text = ""
} else if trayItems.count == 3 {
firstSnapshotLabel.text = trayItems[0]
secondSnapshotLabel.text = trayItems[1]
thirdSnapshotLabel.text = trayItems[2]
fourthSnapshotLabel.text = ""
} else if trayItems.count >= 4 {
firstSnapshotLabel.text = trayItems[0]
secondSnapshotLabel.text = trayItems[1]
thirdSnapshotLabel.text = trayItems[2]
fourthSnapshotLabel.text = trayItems[3]
} else {
firstSnapshotLabel.text = ""
secondSnapshotLabel.text = ""
thirdSnapshotLabel.text = ""
fourthSnapshotLabel.text = ""
}
然后,我手动设置每个标签:
let firstSnapshotLabel: UILabel = {
let label = UILabel()
label.attributes(text:"Item 1", textColor: Colors.appDarkGrey, alignment: .left, font: Fonts.rubikRegular, size: 12, characterSpacing: 0.5, backgroundColor: nil)
return label
}()
let secondSnapshotLabel: UILabel = {
let label = UILabel()
label.attributes(text:"Item 2", textColor: Colors.appDarkGrey, alignment: .left, font: Fonts.rubikRegular, size: 12, characterSpacing: 0.5, backgroundColor: nil)
return label
}()
let thirdSnapshotLabel: UILabel = {
let label = UILabel()
label.attributes(text:"Item 3", textColor: Colors.appDarkGrey, alignment: .left, font: Fonts.rubikRegular, size: 12, characterSpacing: 0.5, backgroundColor: nil)
return label
}()
let fourthSnapshotLabel: UILabel = {
let label = UILabel()
label.attributes(text:"Item 4", textColor: Colors.appDarkGrey, alignment: .left, font: Fonts.rubikRegular, size: 12, characterSpacing: 0.5, backgroundColor: nil)
return label
}()
正如我所说,上面的代码提供了我想要的,但是它非常混乱和低效。
我还尝试了一些不太管用的东西。我假设“快照标签”是在填充“快照”数组之前加载到视图中的。我假设这是因为当我打印“快照”时,我得到了填充的数组,但当我打印“快照标签”时,我得到了一个空数组
var tray: Tray? {
didSet {
guard let trayTitle = tray?.title else { return }
guard let trayItems = tray?.items else { return }
// tray.items will give an array of strings
trayTitleLabel.text = trayTitle
for item in trayItems {
snapshots.append(item)
}
}
}
var snapshots = [] as [String]
lazy var snapshotLabels = snapshots.prefix(4).map { item -> UILabel in
let label = UILabel()
label.attributes(text:"\(item)", textColor: Colors.appDarkGrey, alignment: .left, font: Fonts.rubikRegular, size: 12, characterSpacing: 0.5, backgroundColor: nil)
return label
}
托盘结构和实例数组:
struct Tray {
let title: String
let items: [String]
}
let trays = [Tray(title: "Groceries", items: ["Apples","Water","Milk","Eggs","Fruity Pebbles","Bread","Butter","Flour","Yogurt","Pop-Tarts"]),
Tray(title: "Home", items: ["Laundry","Trash","Clean desk"]),
Tray(title: "Miscellaneous", items: ["Call mom","Feed dogs"]),
Tray(title: "Bucket List", items: ["Skydiving","England","Pilot's license","Learn spanish","Barcelona","Italy"]),
Tray(title: "Dinner", items: ["Pasta","Crabs","Tacos","Pizza","Steak","Cheesesteaks","Chicken and Rice"])
]
有人知道为什么会这样吗?
谢谢D将标签做成一个数组
let labels = (0..<4).map {
let label = UILabel()
label.attributes(text:"Item \($0)", textColor: Colors.appDarkGrey, alignment: .left, font: Fonts.rubikRegular, size: 12, characterSpacing: 0.5, backgroundColor: nil)
return label
}
let labels=(0。。
(0..<trayItems.count).forEach { labels[$0].text = trayItems[$0] }
(trayItems.count..<labels.count).forEach { $0.text = "" }