这是我的数据库结构
"type" : {
"A" : {
"nightAmount" : 5,
"noonAmount" : 2
},
"B" : {
"nightAmount" : 5,
"noonAmount" : 3
},
"C" : {
"nightAmount" : 2,
"noonAmount" : 5
}}
我希望我的tableviewCell显示如下标签: -
第一个单元格:A - 中午安装 - 2
第二格:A - nightAmount - 5
第3单元格:B - 中午安装 - 3
第四格:B - nightAmount - 5
第5个细胞:C - 中午安装 - 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
它只是表现得很奇怪,我猜它是因为程序是从顶部执行到僵尸程序......而不是一次......
是否有人可以告诉我如何改进此代码
我非常确定这是一个更好的方法! :/
答案 0 :(得分:2)
您的逻辑似乎没有问题,只是您会收到编译错误,因为V1
,V2
等在封闭范围内声明。
我会建议这样的事情:
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 :(得分:2)
正如我的评论中所建议的,一种方法是创建一个数据对象来保存每个单元格的内容。这样,您就可以在数据对象和tableview的列表单元格之间进行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))
}
因为您的膳食数组与您的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
}