尝试引用tableView单元格的每个自定义类型项

问题描述

我正在解析JSON文件中的数据,并创建了一个结构来保存该数据。我试图在tableView中显示自定义结构的每个项目,但是我在如何引用每个项目上陷入了困境。

这是我的结构:

struct Country: Codable {

    var id: Int
    var country: String
    var capital: String
    var nationalLanguage: [String]
    var population: Int
    
}

这是我的表视图控制器。现在,我只知道如何在自定义类型中引用单个项目。显然,这会将所有单元格设置为该项目。

class TableViewController: UITableViewController {
    
    var countryItem: Country?
    
    override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cells",for: indexPath)
        
        if let country = countryItem {
            cell.textLabel!.text = String(country.population)
        }
        return cell
    }
        
}

当我打印出countryItem变量时,这就是我得到的:

Country(id: 1,country: "United States",capital: "Washington D.C.",nationalLanguage: ["English"],population: 328239523)

我是否需要以某种方式将其设置为数组,以便可以分别引用每个项目?

解决方法

已更新:

选项1

class TableViewController: UITableViewController {
    
    var countryItem: Country?

    var arrayStrings: [String] {
        guard let countryItem = countryItem else { return [] }
        return [
            "\(countryItem.id)",countryItem.country,countryItem.capital,countryItem.nationalLanguage.joined(separator: ","),"\(countryItem.population)",]
    }
    
    override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return arrayStrings.count
    }
    
    override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cells",for: indexPath)

        cell.textLabel!.text = arrayStrings[indexPath.row]
        return cell
    }
        
}

选项2

class TableViewController: UITableViewController {
    
    var countryItem: Country?
    
    override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return countryItem == nil ? 0 : 5
    }
    
    override func tableView(_ tableView: UITableView,for: indexPath)
        
        let text: String
        switch indexPath.row {
        case 0:
            text = "\(countryItem!.id)"
        case 1:
            text = countryItem!.country
        case 2:
            text = countryItem!.capital
        case 3:
            text = countryItem!.nationalLanguage.joined(separator: ",")
        case 4:
            text = "\(countryItem!.population)"
        default:
            break
        }

        cell.textLabel!.text = text
        return cell
    }
        
}

如果只需要显示结构中所有字段的字符串表示形式,那么可以,将它们转换为单个字符串数组并使用可重复使用的单元格进行渲染。

如果每个字段需要不同的样式,则可能不需要数组,只需创建一些自定义单元格,然后从结构中为其分配数据即可。

或者也许您甚至不需要在这里使用表格视图,因为我看到您只有一个国家/地区,并且这里不需要可重用。只需创建带有5个(也许)标签的自定义视图并显示数据即可。

由于某些原因,您仍然需要一个表格视图,然后创建一个可以显示country的所有信息的单元格。

,

希望它可以为您提供帮助。

class TableViewController: UITableViewController {

    var countryItem = [Country]()

    override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return countryItem.count
    }

    override func tableView(_ tableView: UITableView,for: indexPath)
            cell.textLabel!.text = countryItem[indexPath.row].country
    
        return cell
    }
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...