0

This is a really basic wireframe of what I'm trying to achieve:

enter image description here

This is what I currently have in Xcode, resulting in 3 cells per row, with no space in between. The date label cell isn't showing up because I don't know how to get cellForItem to recognize it along with the post cells (hence the question):

enter image description here

I have two separate cell classes - one for the date labels and one for the rows of images.

My collection view methods are as follows:

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return posts.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! PostCell

    let dateCell = collectionView.dequeueReusableCell(withReuseIdentifier: "dateLabelCell", for: indexPath) as! DateLabelCell

    cell.postImage.loadImageUsingCacheWithUrlString(posts[indexPath.row].pathToImage)
    cell.postID = posts[indexPath.row].postID
    cell.postImage.contentMode = UIViewContentMode.scaleAspectFill

    // Here I'll have to get the date and have the labels display the days of the week properly
    dateCell.dateLabel.text = "Monday"

    return cell
}

But I'm not sure how to manage both cells in the above methods. I'll have to sort the posts by a timestamp so things posted on a certain day get added to the correct day's row, but that's something I'll do another day - for now I'm just wondering how I can get the UI laid out so that the collection view shows a date label, then a row of post cells, and repeat.

Thanks for any advice!

KingTim
  • 1,281
  • 4
  • 21
  • 29
  • 2
    Why not use sections for each day? Then, you could just check which section you're on, override the titleForSection: delegate method, and return the correct day. Is there anything else that needs to go in the dateLabelCell? Even if there is you could override sectionHeader for collectionView and return a custom header. – Jonathan Apr 03 '17 at 14:37
  • As @Jonathan said, you must use Sections for each day. And for the "Day Label" you must use the header of the section (sectionForHeader) – Aitor Pagán Apr 03 '17 at 14:41
  • http://stackoverflow.com/questions/20910105/uicollectionview-with-section-headers-like-a-uitableview http://stackoverflow.com/questions/28102639/uicollectionview-multiple-sections-and-headers http://stackoverflow.com/questions/33488743/how-to-add-header-for-section-in-collection-view-on-top-below – Aitor Pagán Apr 03 '17 at 14:43
  • Just for the sake of answering the actual question, since the data is sorted you could keep track of which day you're on, compare the day on the current post to the current day, then if the day is different return the dateLabelCell with that day instead of the postCell. However, you'll also have to offset the indexPath to account for the day cells. You'll also need to make sure the data is sorted Sun. - Sat.. – Jonathan Apr 03 '17 at 14:48
  • Thanks guys, I'll take a look at those links and see what I can do. I don't have any experience using sections, I'm still getting used to using collection views. But that seems like a sensible way to do what I'm trying to do. – KingTim Apr 03 '17 at 14:59

0 Answers0