[英]Issue in showing multiple cells in tableview
我有兩個字段,分別是生日和周年紀念日。 現在,如果生日或周年紀念日在接下來的30天內到來,則必須在表格視圖中顯示它們。 我正在努力實現這樣的目標。
let cell2: BestWishesTableViewCell = tableView.dequeueReusableCell(withIdentifier: "bdayWishesCellIdentifier") as! BestWishesTableViewCell
if indexPath.row < customerDetails.count {
let bdayObj = customerDetails[indexPath.row]
if Calendar.current.isDateInNextThirtyDays(bdayObj.birthday!) {
cell2.nameLabel.text = bdayObj.fname
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
cell2.dateLabel.text = formatter.string(from: bdayObj.birthday!)
} else if Calendar.current.isDateInNextThirtyDays(bdayObj.anniversary!) {
cell2.nameLabel.text = bdayObj.fname
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
cell2.dateLabel.text = formatter.string(from: bdayObj.anniversary!)
}
}
else {
let bdayObj = customerDetails2ArrTwo[indexPath.row - customerDetailsArrTwo.count]
if Calendar.current.isDateInNextThirtyDays(bdayObj.birthday!) {
cell2.nameLabel.text = bdayObj.fname
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
cell2.dateLabel.text = formatter.string(from: bdayObj.birthday!)
} else if Calendar.current.isDateInNextThirtyDays(bdayObj.anniversary!) {
cell2.nameLabel.text = bdayObj.fname
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
cell2.dateLabel.text = formatter.string(from: bdayObj.anniversary!)
}
return cell2
}
在第一部分中,從if indexPath.row < customerDetails.count
到else
部分之前,我從一個表中獲取數據,在其他部分中,我從另一個表中獲取數據。 在我剛剛提到的第一部分中,我正在檢查生日和周年紀念日是否在接下來的30天內(現在,實際上bday和周年紀念日都在接下來的30天內,因為這就是我給出的信息)。 但是它僅在表視圖中顯示生日,而不是周年紀念日,而兩個日期都應該在表視圖中顯示。
現在,既然我已經給出了else if
我就知道它只會執行第一個if
條件。 但是,即使我不使用else if
,而是使用另一個if
,然后發生的事情還是只顯示了一個單元格(即一個日期)(這一次是周年紀念日),因為生日被周年紀念日日期覆蓋了。
如何使生日和周年紀念日一起出現(即2個單元格)...?
您可以執行以下操作。 基本上,您希望customerDetails數組中的每個對象都有兩行。 然后,使用%2行檢查以確定它是偶數還是奇數
if indexPath.row < customerDetails.count * 2 {
let bdayObj = customerDetails[indexPath.row]
if indexPath.row % 2 == 0 {
if Calendar.current.isDateInNextThirtyDays(bdayObj.birthday!) {
cell2.nameLabel.text = bdayObj.fname
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
cell2.dateLabel.text = formatter.string(from: bdayObj.birthday!)
}
} else {
if Calendar.current.isDateInNextThirtyDays(bdayObj.anniversary!) {
cell2.nameLabel.text = bdayObj.fname
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
cell2.dateLabel.text = formatter.string(from: bdayObj.anniversary!)
}
}
}
但是,當您在接下來的30天內有生日或周年紀念日(即不能同時使用這兩種方法)時,這種方法將無法正常工作。
老實說,我認為您需要修改自己的操作方式。 您應該構建一個干凈的數組,其中包含一個用於生日或周年紀念日的項目(如果需要,則包含2個項目),並且已經在接下來的30天內對其進行了檢查。 這樣,您就可以基於indexPath.row在cellforItemAt中進行干凈的查找。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.