如何显示nil,或者使其不显示表视图中类的某些实例?

huangapple go评论54阅读模式
英文:

How do I display nil, or make it not display certain instances of a class in a table view?

问题

我有一个文件,其中有一个附加了几个变量的类,基本上我想要能够只显示类的那些具有大于0的数量的部分,而不显示空值。这是非常重要的,因为这是一个较大应用的一部分,如果我这样做,将会导致应用崩溃。

到目前为止,我只是在处理这个单元格。

我的类数组和代码:

var allResources = [m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, m16, m17, m18, m19, m20, m21, m22, m23, m24, m25, m26, m27, m28, m29, m30, m31, m32, m33, m34, m35, m36, m37, m38, m39, m40, m41, m42, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16, f17, f18, f19, f20, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10]


@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // 在加载视图后执行任何额外的设置。
    f3.amount += 1
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath)
    if allResources[indexPath.row].amount > 0 {
        cell.textLabel?.text = "\(allResources[indexPath.row].name)"
        cell.detailTextLabel?.text = "\(allResources[indexPath.row].amount)"
    }
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    allResources.remove(at: 0)
    tableView.deleteRows(at: [indexPath], with: .fade)
}

我的类的一部分:

class resource {
    var name: String
    var type: String
    var value: Int
    var edible: Bool
    var buildingMaterial: Bool
    var craftsMaterial: Bool
    var toolMaterial: Bool
    var amount: Int = 0
    
    init(name: String, type: String, value: Int, edible: Bool, buildingMaterial: Bool, craftsMaterial: Bool, toolMaterial: Bool, amount: Int) {
        self.name = name
        self.type = type
        self.value = value
        self.edible = edible
        self.buildingMaterial = buildingMaterial
        self.craftsMaterial = craftsMaterial
        self.toolMaterial = toolMaterial
        self.amount = amount
    }
}

// 石灰石
var m1 = resource(name: "Limestone", type: "Mineral", value: 1, edible: false, buildingMaterial: true, craftsMaterial: true, toolMaterial: true, amount: 0)
// 花岗岩
var m2 = resource(name: "Granite", type: "Mineral", value: 20, edible: false, buildingMaterial: true, craftsMaterial: true, toolMaterial: false, amount: 0)
英文:

I have a file that has a class with a few variables attached to it, and essentially I want to be able to only display the parts of the class that have an amount higher than 0 without still displaying the empty values. It's very important that I do this without programmatically assigning a new array as this is part of a larger app that would break if I did that

So far I have just been messing with the cell

My class array and code

var allResources = [m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, m16, m17, m18, m19, m20, m21, m22, m23, m24, m25, m26, m27, m28, m29, m30, m31, m32, m33, m34, m35, m36, m37, m38, m39, m40, m41, m42, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16, f17, f18, f19, f20, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10]


@IBOutlet weak var tableView: UITableView!
    
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    f3.amount += 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return allResources.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath)
    if allResources[indexPath.row].amount > 0 {
        cell.textLabel?.text =  "\(allResources[indexPath.row].name)"
        cell.detailTextLabel?.text = "\(allResources[indexPath.row].amount)"
        
    }
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    allResources.remove(at: 0)
    tableView.deleteRows(at: [indexPath], with: .fade)
}

A part of my class

class resource {
    var name: String
    var type: String
    var value: Int
    var edible: Bool
    var buildingMaterial: Bool
    var craftsMaterial: Bool
    var toolMaterial: Bool
    var amount: Int = 0
    
    init(name: String, type: String, value: Int, edible: Bool, buildingMaterial: Bool, craftsMaterial: Bool, toolMaterial: Bool, amount: Int) {
        self.name = name
        self.type = type
        self.value = value
        self.edible = edible
        self.buildingMaterial = buildingMaterial
        self.craftsMaterial = craftsMaterial
        self.toolMaterial = toolMaterial
        self.amount = amount
    }
}

//Limestone
var m1 = resource(name: "Limestone", type: "Mineral", value: 1, edible: false, buildingMaterial: true, craftsMaterial: true, toolMaterial: true, amount: 0)
//Granite
var m2 = resource(name: "Granite", type: "Mineral", value: 20, edible: false, buildingMaterial: true, craftsMaterial: true, toolMaterial: false, amount: 0)

答案1

得分: 1

以下是您要翻译的内容:

"It seems you want to filter your array. Usually you would just remove the values you don't need from the allResources array. But if the array is used in a different context you are pretty much left with 1 option here.

Create a new array that contains only the values you want to use. Possible implementation:

var resourcesWithAmount: [resource]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    f3.amount += 1
    resourcesWithAmount = allResources.filter{$0.amount > 0 }
}

and use only this collection as datasource for your tableview:

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath)

    cell.textLabel?.text = "\(resourcesWithAmount[indexPath.row].name)"
    cell.detailTextLabel?.text = "\(resourcesWithAmount[indexPath.row].amount)"

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // this should probably read
    // resourcesWithAmount.remove(at: indexpath.row)
    resourcesWithAmount.remove(at: 0)
    tableView.deleteRows(at: [indexPath], with: .fade)
}
```"

<details>
<summary>英文:</summary>

It seems you want to filter your array. Usually you would just remove the values you don&#180;t need from the `allResources` array. But if the array is used in a different context you are pretty much left with 1 option here.

Create a new array that contains only the values you want to use. Possible implementation:

    var resourcesWithAmount: [resource]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        f3.amount += 1
        resourcesWithAmount = allResources.filter{$0.amount &gt; 0 }
    }

and use only this collection as datasource for your tableview:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
        return resourcesWithAmount.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: &quot;CellID&quot;, for: indexPath)
        
        cell.textLabel?.text =  &quot;\(resourcesWithAmount[indexPath.row].name)&quot;
        cell.detailTextLabel?.text = &quot;\(resourcesWithAmount[indexPath.row].amount)&quot;
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // this should probably read
        // resourcesWithAmount.remove(at: indexpath.row)
        resourcesWithAmount.remove(at: 0)
        tableView.deleteRows(at: [indexPath], with: .fade)
    }


</details>



huangapple
  • 本文由 发表于 2023年2月16日 03:56:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定