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

标签 ios swift uitableview

我有两个字段,分别输入生日和周年纪念日。现在,如果生日或周年纪念日在接下来的 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/

相关文章:

ios - CoreData实体使用合并策略作为NSMergeByPropertyObjectTrumpMergePolicy保存/更新,从而在Xcode中抛出编译器错误?

ios - 检查来自 Apple Sign in 的电子邮件是否已在 Firebase 中注册

ios - UITableView 单元格自动布局约束

iphone - 如何在 iPhone 向下钻取应用程序中处理无限数量的 TableView

ios - UITableView/UIScrollView 改变手指抬起后弹跳的速度

ios - 在 TextView 中显示自定义文本

ios - 从 Objective C 中的另一个类访问变量值

objective-c - UIView 未检测到横向触摸

ios - 错误处理、持久容器、通知 IOS

ios - 自动续订 App Store 订阅需要“恢复购买”吗?