Swift 如何仅当我的数据库中有值时才显示 tableview 单元格

Swift How do I show tableview cell only if there is value in my database

这是我的数据库结构

"type" : {
      "A" : {
        "nightAmount" : 5,
        "noonAmount" : 2
      },
      "B" : {
        "nightAmount" : 5,
        "noonAmount" : 3
      },
      "C" : {
        "nightAmount" : 2,
        "noonAmount" : 5
      }}

我希望我的 tableviewCell 显示如下标签:-

第一个单元格:A - noonAmount - 2

第二个单元格:A - nightAmount - 5

第 3 个单元格:B - noonAmount - 3

第 4 个单元格:B - nightAmount - 5

第 5 个单元格:C - noonAmount - 5

第 6 个单元格:C - nightAmount - 2

但是金额可以为 0,我不希望金额为 0 的类型显示在我的 tableview 单元格上,

我想在 cellforrowat 函数中调用什么?

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

    let cell =  tableView.dequeueReusableCell(withIdentifier: "confCell")! as UITableViewCell {  

这是我的 numberofrowsinsection

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {  
    if AnoontAmount >= 1 {
        let V1 = 1
    } else { V1 = 0 }

    if ANightAmount >= 1 {
        let V2 = 1
    } else { V2 = 0 }

    if BnoonAmount >= 1 {
        let V3 = 1
    } else { V3 = 0 }

    if BNightAmount >= 1 {
        let V4 = 1
    } else { V4 = 0 }

    if CnoonAmount >= 1 {
        let V5 = 1
    } else { V5 = 0 }

    if CNightAmount >= 1 {
        let V6 = 1
    } else { V6 = 0 }


    let cellTotal = V1 + V2 + V3 + V4 + V5 + V6
    return cellTotal

不知道行不行

谁能告诉我是否有正确的方法来完成这项工作?

谢谢!!

更新

我更新了我的代码如下

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    var cellTotal = 0

    if  V1amount > 0 {
        cellTotal += 1
    }

    if  V2amount > 0 {
        cellTotal += 1
    }

    if  V3amount > 0 {
        cellTotal += 1
    }


    if  V4amount > 0 {
        cellTotal += 1
    }


    if  V5amount > 0 {
        cellTotal += 1
    }

    if  V6amount > 0 {
        cellTotal += 1
    }

    return cellTotal

}

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


    var mealtype : Array = ["TypeA", "TypeA", "TypeB", "TypeB", "TypeC", "TypeC"]
    var mealtime : Array = ["lunch", "dinner", "lunch","dinner","lunch","dinner"]
    var mealamount : Array = ["\(V1amount!)","\(V2amount!)","\(V3amount!)","\(V4amount!)","\(V5amount!)","\(V6amount!)"]

    if V1amount == 0 {
        mealtype.remove(at: 0)
        mealtime.remove(at: 0)
        mealamount.remove(at: 0)
    }

    if V2amount == 0 {
        mealtype.remove(at: 1)
        mealtime.remove(at: 1)
        mealamount.remove(at: 1)
    }


    print (mealtype)
    print (mealtime)
    print (mealamount)




    if let cell = tableView.dequeueReusableCell(withIdentifier: "confCell") as! confCell! {

        cell.confMealName.text = mealtype[(indexPath as NSIndexPath).row]
        cell.confNN.text = mealtime[(indexPath as NSIndexPath).row]
        cell.confAmount.text! = mealamount[(indexPath as NSIndexPath).row]

        return cell
    } else {
        return confCell()
    }

}

现在的问题是,如果只有一个VAmount = 0,它就可以正常工作 我删除了数组中的索引,这样它就不会显示 tableview 单元格 但是如果有两个或多个 VAmount = 0

它只是表现得很奇怪,我猜这是因为程序是从头到尾执行的...而不是一次...

有谁能告诉我如何改进这段代码

我很确定有更好的方法来做到这一点! :/

你的逻辑似乎没问题,除了你会得到编译错误,因为 V1V2 等是在封闭范围内声明的。

我建议这样:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    var cellTotal = 6

    guard AnoonAmount > 0 else {cellTotal -= 1}

    guard ANightAmount > 0 else {cellTotal -= 1}

    guard BnoonAmount > 0 else {cellTotal -= 1}

    guard BNightAmount > 0 else {cellTotal -= 1}

    guard CnoonAmount > 0 else {cellTotal -= 1}

    guard CnightAmount > 0 else {cellTotal -= 1}

    return cellTotal      
}

正如我在评论中所建议的,一种方法是创建一个数据对象来保存每个单元格的内容。这样,您在数据对象和表格视图的列表单元格之间就有了 1-1 映射。

首先,创建一个数组来保存包含非零量膳食的对象。这应该在您从数据库中获取 JSON 的地方完成(即不在 cellForRowAtIndexPath 内)。在这种情况下,为了简单起见,我将使用 Swift 元组作为数据对象,但您始终可以使用普通的旧 Swift 对象。

// An array of Meal tuples, each of which can hold a type, time and amount)
var meals:[(type: String, time: String, amount: Double)]  = []

对于每个膳食对象,如果数量为空,我们干脆不将其添加到数组中:

if v1amount > 0 {
    meals.append((type: "TypeA", time: "lunch", amount: v1amount))
}
if v2amount > 0 {
    meals.append((type: "TypeA", time: "dinner", amount: v2amount))
}
if v3amount > 0 {
    meals.append((type: "TypeB", time: "lunch", amount: v3amount))
}
if v4amount > 0 {
    meals.append((type: "TypeB", time: "dinner", amount: v4amount))
}
if v5amount > 0 {
    meals.append((type: "TypeC", time: "lunch", amount: v5amount))
}
if v6amount > 0 {
    meals.append((type: "TypeC", time: "dinner", amount: v6amount))
}

因为您的 meals 数组和 tableview 之间存在 1-1 映射,所以实现 numberOfRowsInSection 委托方法变得简单:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // return the number of meals in your array
    return meals.count
}

类似地,对于 cellForRowAtIndexPath,您可以简单地从相关索引处的数组中获取对象,并呈现其内容:

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "confCell") as! confCell
    // Get the meal object for this row
    let meal = meals[indexPath.row]

    // Populate the fields based on the meal's property
    cell.confMealName.text = meal.type
    cell.confNN.text = meal.time
    cell.confAmount.text! = meal.amount

    return cell
}