当白天更改时,在UICollectionViewController中创建Datestamp单元格

时间:2017-08-06 09:20:23

标签: ios swift

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

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

任何解决方案?

1 个答案:

答案 0 :(得分:0)

我建议您使用二维数组,并清楚地说明保存日期和消息的位置。一个很好的起点可能是:

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
}