ios - 日期更改时在 UICollectionViewController 中创建日期戳单元格

标签 ios swift

创建聊天应用程序,如 iMessage,但不确定如何在日期更改时包含包含日期的单元格。我尝试使用 collectionViewCell 的标题,但它似乎没有用。这是我添加 header 的代码。

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    let reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView", for: indexPath) as! HCollectionReusableView
    reusableview.translatesAutoresizingMaskIntoConstraints = false
    if(indexPath.item > 1){
    let timestamp1 = (messages[indexPath.item ].timestamp?.doubleValue)!
    let timestamp2 = (messages[indexPath.item - 1].timestamp?.doubleValue)!
    if NSCalendar.current.isDate(Date(timeIntervalSince1970: timestamp1), inSameDayAs: Date(timeIntervalSince1970: timestamp2)) == false {
        reusableview.heightAnchor.constraint(equalToConstant: 20).isActive = true
        switch kind {
        case UICollectionElementKindSectionHeader:
            let calendar = NSCalendar.current
            let date = Date(timeIntervalSince1970: (messages[indexPath.item ].timestamp?.doubleValue)!)
            if calendar.isDateInYesterday(date) {
                reusableview.DateStamp.text = "Yesterday"
            }
            else if calendar.isDateInToday(date) {
                reusableview.DateStamp.text = "Today"
            }
            else{
                if let seconds = messages[indexPath.item].timestamp?.doubleValue {
                    let timestampDate = Date(timeIntervalSince1970: seconds)

                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "MM/dd/yyyy"
                    reusableview.DateStamp.text = dateFormatter.string(from: timestampDate)
                }
                print(reusableview.DateStamp.text!)
                }
                default:  fatalError("Unexpected element kind")
            }
            return reusableview
        }}
    reusableview.DateStamp.text = ""
    reusableview.heightAnchor.constraint(equalToConstant: 0).isActive = true
    return reusableview
}

但是标题没有出现。 Screenshot Attached. The date should appear in the middle like imessage

有什么解决办法吗?

最佳答案

我建议你使用一个二维数组,让自己清楚在哪里保存日期和消息。一个好的起点可以是:

var items: [[CustomType?]] = []

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print("section \(section)")
        print("items count \(items[section].count)")
        return items[section].count
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return items.count
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
                                                                     withReuseIdentifier: "sectionHeader",
                                                                     for: indexPath) as! HeaderCollectionReusableView
    if items[indexPath.section].count >= 1 {
        if let sectionId = items[indexPath.section][0] {
            let date: String = sectionId.id
            headerView.dateLabel.text = date
            return headerView
        }
    }
    headerView.dateLabel.text = "fix it"
    return headerView
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
    guard let currentImage = items[indexPath.section][indexPath.item] else {
        //configure
        return cell
    }
    cell.configure(withPicture: currentImage)
    return cell
}

关于ios - 日期更改时在 UICollectionViewController 中创建日期戳单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45530531/

相关文章:

javascript - 用户删除并重新安装应用程序后管理 Parse _Installation 类

ios - 在 iOS 中使用 Storyboard View 中的模糊背景

ios - 是否可以直接跳转到 UIActivityViewController 中的共享扩展之一?

swift - 将自动计时器 PFObject 添加到 Parse.com 类

ios - 来回导航时清理旧的 UITableViewCells

ios - 我们如何在移动设备(iOS 和 Android)上播放 Scorm 内容?

ios - 适用于 iOS 的 Google Plus API : Share function - Crash app when touching "Public" in the native dialog share?

ios - 如何在 Xcode Assets 目录中分配启动图像?

ios - 评论主 Storyboard上的 View

ios - 以编程方式显示和隐藏容器 UIView 及其 subview