如何过滤UITableView中显示的信息类型?迅捷JSON [英] How to filter what type of information is shown in a UITableView | Swift JSON
问题描述
我有一个UITableView
从JSON获取信息,问题是我只想显示UITableView
中的String
值,但不包括任何Doubles
I have a UITableView
that is getting information from JSON the issue is I would like to only show the String
values in the UITableView
excluding any Doubles
JSON解码器的结构为:
The structure of the JSON decoder is:
struct PurchaseTotals: Codable {
var sumTotal: Double?
var animal: String?
var name: String?
enum CodingKeys: String, CodingKey {
case sumTotal = "sumTotal"
case animal = "animal"
case sumTotal = "name"
}
lazy var formattedSumTotal: String? = {
if let sumTotal = sumTotal {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
let str = formatter.string(from: NSNumber(value: sumTotal))
return str
}
return nil
}()
}
在cellForRowAt
中,我用变量portfolio
定义结构:
And in the cellForRowAt
I define the structure with the variable portfolio
:
var portfolio: PurchaseTotals
cellForRowAt
中的单元格正在填充,并通过以下方式在UITableView中显示.
And in cellForRowAt
the cells are being populated and are being shown in the UITableView the following way.
首先,我使用变量structure
定义JSON结构:
First I define the JSON structure with the variable structure
:
var structure = [PurchaseTotals]()
然后我将变量portfolio
设置为等于structure[indexPath.row]
Then I set the variable portfolio
equal to structure[indexPath.row]
portfolio = structure[indexPath.row]
JSON如下:
[{"name":"Jake","animal":"dog"},{"name":"Mary","animal":"cat"},{"sumTotal":100}]
现在的平均单元数为3,但我希望有2个单元,不包括sumTotal.
Meaning the number of cells right now is 3 but I would like 2 cells, excluding the sumTotal.
更新:
import UIKit
class TableViewController: UITableViewController {
var dataSource = [PurchaseTotals]()
var structure = [PurchaseTotals]() {
didSet {
dataSource = structure.filter({ $0.sumTotal == nil })
self.tableView.reloadData()
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! ScheduleCell
var portfolio = dataSource[indexPath.row]
return cell
}
struct PurchaseTotals: Codable {
var sumTotal: Double?
var animal: String?
var name: String?
lazy var formattedSumTotal: String? = {
if let sumTotal = sumTotal {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
let str = "$" + formatter.string(from: NSNumber(value: sumTotal))!
return str
}
return nil
}()
}
推荐答案
创建一个名为dataSource
的单独变量,该变量仅包含structure
中具有sumTotal = nil
的那些元素,即
Create a separate variable named dataSource
that will contain only those elements from structure
having sumTotal = nil
, i.e
var structure = [PurchaseTotals]() {
didSet {
dataSource = structure.filter({ $0.sumTotal == nil })
self.tableView.reloadData()
}
}
var dataSource = [PurchaseTotals]()
在上面的代码中,在获取API响应后填充structure
array
时,dataSource
array
将在structure
的didSet
中更新.
In the above code, the dataSource
array
will be updated in didSet
of structure
when the structure
array
is filled after getting the API response.
因此,structure
将包含来自API响应的实际数据.并且dataSource
将包含来自structure
的已过滤内容.
So, structure
will contain the actual data from API response. And dataSource
will contain the filtered content from structure
.
现在,在
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let portfolio = dataSource[indexPath.row]
//rest of the code...
}
注意:如果属性名称与 API密钥完全匹配,则不需要enum CodingKeys
.因此struct PurchaseTotals
可以重写为
Note: There is no need for enum CodingKeys
if the property names exactly match the API keys. So the struct PurchaseTotals
can be rewritten as,
struct PurchaseTotals: Codable {
var sumTotal: Double?
var animal: String?
var name: String?
lazy var formattedSumTotal: String? = {
//rest of the code...
}()
}
使用示例 JSON 代替 API 这样的响应,
Use sample JSON instead of API response like,
let str = """
[{"name":"Jake","animal":"dog"},{"name":"Mary","animal":"cat"},{"sumTotal":100}]
"""
if let data = str.data(using: .utf8) {
do {
structure = try JSONDecoder().decode([PurchaseTotals].self, from: data)
print(structure)
} catch {
print(error)
}
}
这是对我来说很好的整个代码,
Here is the whole code that is working fine for me,
struct PurchaseTotals: Codable {
var sumTotal: Double?
var animal: String?
var name: String?
lazy var formattedsumTotal: String? = {
if let sumTotal = sumTotal {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
let str = formatter.string(from: NSNumber(value: sumTotal))
return str
}
return nil
}()
}
class TableViewController: UITableViewController {
var structure = [PurchaseTotals]() {
didSet {
dataSource = structure.filter({ $0.sumTotal == nil })
self.tableView.reloadData()
}
}
var dataSource = [PurchaseTotals]()
override func viewDidLoad() {
super.viewDidLoad()
let str = """
[{"name":"Jake","animal":"dog"},{"name":"Mary","animal":"cat"},{"sumTotal":100}]
"""
if let data = str.data(using: .utf8) {
do {
structure = try JSONDecoder().decode([PurchaseTotals].self, from: data)
} catch {
print(error)
}
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = dataSource[indexPath.row].name
return cell
}
}
这篇关于如何过滤UITableView中显示的信息类型?迅捷JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!