我如何将这些单独的标签压缩到我的自定义UICollectionView单元格中的标签数组中?

问题描述 投票:0回答:1

此代码工作得很好,但是我假设有一种更有效的方法来执行这样的操作。我将字符串数组的前四项(此列表可以包含无限量的变量)提供给用户作为“快照”视图。下面的代码在自定义单元格类的内部。

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
}()

正如我说的,上面的代码为我提供了我想要的东西,但是它非常混乱且效率低下。这是我尝试过的其他方法,但效果不佳。我假设在填充“ snapshots”数组之前将“ snapshotLabels”加载到视图中。我认为这是因为当我打印“快照”时会得到填充的数组,但是当我打印“ snapshotLabels”时会得到一个空数组。

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

arrays swift uicollectionview uicollectionviewcell
1个回答
0
投票

使标签成为数组

  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
  }

然后遍历您的托盘项目以在相应的标签上设置值

  (0..<trayItems.count).forEach { labels[$0].text = trayItems[$0] }
  (trayItems.count..<labels.count).forEach { $0.text = "" }
© www.soinside.com 2019 - 2024. All rights reserved.