如何在单个表视图中显示多个属性?斯威夫特

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

我想在表视图上显示核心数据中的三个不同实体。我可以使用一个实体来执行此操作,并尝试将该逻辑传递给在同一表视图上显示三个实体。我在下面的代码中使用了相同的实体进行测试。我的表格视图中出现空白部分。我必须把cellForRowAt方法弄错了吗?这是我的代码。

        var word: [NSManagedObject] = []
        let sections = ["Custom Library", "Mastered Words", "Library"]

        var array = [
              [NSManagedObject](),
              [NSManagedObject](),
              [NSManagedObject]()
              ]


        override func viewDidLoad() {
                super.viewDidLoad()

            tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell2")

        array = [
             word,
             word,
             word
            ]}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array[section].count
}


func numberOfSections(in tableView: UITableView) -> Int {
    sections.count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return self.sections[section]

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let customWord = array[indexPath.row][indexPath.section]
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath)
            cell.textLabel?.text = customWord.value(forKeyPath: "title") as? String
            return cell
swift
1个回答
0
投票

根据您显示的代码,您的array属性是一个空数组,因为word是一个空数组本身。如果您要使用array根据该节索引处的数组确定每个节中的行数,则每个节将获得0行。

© www.soinside.com 2019 - 2024. All rights reserved.