이 내 데이터베이스 구조스위프트는 어떻게 내 데이터베이스에 값이있는 경우에만있는 tableview 셀을 표시 할
"type" : {
"A" : {
"nightAmount" : 5,
"noonAmount" : 2
},
"B" : {
"nightAmount" : 5,
"noonAmount" : 3
},
"C" : {
"nightAmount" : 2,
"noonAmount" : 5
}}
내 tableviewCell 같은 레이블을 표시 할 수 있습니다 : -
1 차 전지 : A - noonAmount을 - 2
2 차 전지 : A - nightAmount - 5
3 셀 : B - noonAmount - 3
4 셀 : B - nightAmount - 5
5 세포 : C - noonAmount - 5
6 셀 : C - nightAmount - 2
그러나 양이 0이 될 수 있으며, 내가 원하지 않는 금액이 내 tableview 셀에 표시하려면 0입니다.
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
이
이사람이이 작업을 수행하려면 적절한 방법이 있는지 말해 줄 수 거 일이 있는지 모르겠어요?
감사합니다. 하나의 VAmount = 0이 있는지
UPDATE는
나는 그렇게 배열의 인덱스를 제거 그냥 잘 작동, 이제 다음
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이 두 개 이상인 경우
그냥 이상하게 작동합니다. 프로그램이 실행되기 때문에 그럴 것 같습니다. f 롬 봇에 위로 ...이 아니라 한 번에 ...
방법이 코드를이 할 수있는 더 나은 방법이있다 내가 확신을 개선하기 위해 말해 줄 수 사람이 있는가! :/
실제로 원하십니까? 셀에 값 0 표시 안 함 –
각 셀의 내용을 나타내는 데이터 개체 (예 : 이름, 시간, 양)를 만들 것을 제안합니다. json을 수신하면이를 데이터 오브젝트 목록에 따라 구문 분석하십시오. 금액이 비어 있으면 목록에 추가하지 마십시오. 이렇게하면 데이터 객체와 tableview의 list 셀, 즉 numberOfRowsInSection에 대한 1-1 매핑이 생기며 간단히 list.count를 반환 할 수 있습니다. cellForRowAtIndexPath의 경우 [indexPath.row] 목록으로 데이터 객체를 잡고 값에 따라 셀을 렌더링 할 수 있습니다. –
@Jecky, 예. 예를 들어/A/noonAmount = 0 인 경우 5 행만 갖습니다. 그 모든 6 행을 가지고 쉽게, 난 그냥 0> 0 값을 가진 사람들을 보여줄 방법이 없어. – Ian