在 tableview 中显示多个单元格的问题 [英] Issue in showing multiple cells in tableview

查看:24
本文介绍了在 tableview 中显示多个单元格的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字段,分别是生日和周年纪念日.现在,如果生日或周年纪念日在接下来的 30 天内出现,我必须将它们显示在 tableview 中.这是我正在努力实现的目标..

I have two fields which takes the birthdate and anniversary date. Now if the birthdate or anniversary date comes within the next 30 days, I have to display them in a tableview. This I'm trying to achieve like so..

    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 部分之前,我从一个表中获取数据,在 else 部分中我从另一个表中获取数据.在我刚刚提到的第一部分中,我正在检查生日和周年纪念日期是否在接下来的 30 天内(现在实际上生日和周年纪念日期都在接下来的 30 天内,因为那是我给出的).但它只在 tableview 中显示生日,而不是周年日期,而这两个日期都应该显示在 tableview 中.

Here, in the first part, from if indexPath.row < customerDetails.count till before the else part, I take data from one table and in the else part I take data from another table. In the first part that I just mentioned, I'm checking if the birthday and anniversary date is within the next 30 days(Now in fact both bday and anniversary dates are within the next 30 days because thats what I've given). But it is showing just the birthday in the tableview and not the anniversary date while both dates should have been shown in the tableview.

既然我已经给出了 else if,我知道它只会执行第一个 if 条件.但即使我没有使用 else if 而是使用另一个 if ,然后发生的事情也只是显示了一个单元格(即一个日期)(这次是周年日期显示),因为生日被周年日期覆盖.

Now since I've given else if I know it will execute just the 1st if condition. But even if I didn't use else if and used another if instead, then what happened was again just one cell(i.e. one date) was shown (and this time the anniversary date was shown) as the birthdate was overwritten by the anniversary date.

如何让生日和纪念日同时出现(即 2 个单元格)...?

How can I make both birthdate and anniversary to appear together(i.e. 2 cells)...?

推荐答案

您可以执行以下操作.基本上,您期望 customerDetails 数组中的每个对象都有两行.然后,使用行 %2 检查来确定它是偶数还是奇数

You could do something like the below. Basically, you're expecting two rows for each object in the customerDetails array. Then, use the row % 2 check to determine if it's even or odd

    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 天内有生日或周年纪念日(即两者都不是),这将不会奏效.

However, that's not going to work well when you have a birthday or an anniversary in the next 30 days (i.e. not both).

老实说,我认为您需要修改您的做法.您应该构建一个干净的数组,其中包含一个生日或周年纪念日的项目(如果需要,2 个项目),如果它在接下来的 30 天内已经检查过.这样,您就可以根据 indexPath.row 在 cellforItemAt 中进行干净的查找.

To be honest with you, I think you need to revise how you're doing this. You should build a clean array that contains one item for either birthday or anniversay (2 items if needed) which it's already checked if its in the next 30 days. This way, you can just have a clean lookup in your cellforItemAt based upon indexPath.row.

这篇关于在 tableview 中显示多个单元格的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆