gpt4 book ai didi

ios - 在表格 View 中显示多个单元格时出现问题

转载 作者:行者123 更新时间:2023-11-30 11:38:20 25 4
gpt4 key购买 nike

我有两个字段,分别输入生日和周年纪念日。现在,如果生日或周年纪念日在接下来的 30 天内,我必须将它们显示在表格 View 中。我正在努力实现这一点......

    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 天内(现在实际上生日和周年纪念日期都在接下来的 30 天内,因为这就是我给出的)。但它只在表格 View 中显示生日,而不是周年纪念日,而这两个日期都应该显示在表格 View 中。

既然我已经给出了 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 中进行干净的查找。

关于ios - 在表格 View 中显示多个单元格时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49493458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com