needing to get count of the array in dictionary for my CollectionView

Viewed 3196

Ok, I'm trying to make my CollectionView with each section having a title and within each section is 3 imageviews with their corresponding label. I have the image views matched up to the labels, but I'm having a problem being able to return the proper numberOfItemsInSection. I've changed my code so many times in an attempt to fix this, I'm not even sure what is right within the rest of my app, but in essence this is the important part:

var sectionsArrayDictionary : [String:Array<String>] = ["level 1" : ["item1", "item2", "item3"], "level 2" : ["item4","item5", "item6"], "level 3" : ["item7", "item8", "item9"]]

I can not figure out a way to access the values so that I can return a count. (And as an extra help if anyone knows how to fix: my pictures are showing the same 3 images in each section, instead of each section showing its own pictures. I'm hoping this will resolve when the above part gets fixed.)

update: my code is a mess, but since you would like more code here it is. There are two different tutorials combined at the moment, so it may confuse you more.

import UIKit

let reuseIdentifier = "collCell"
let headerViewIdentifier = "headerView"
var sections: [Section] = SectionsData().getSectionsFromData()

class LayoutController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    let sectionInsets = UIEdgeInsets(top: 5.0, left: 5.0, bottom: 5.0, right: 5.0)
    var animalLabels = ["max", "roxy", "buster", "daisy", "charlie", "maggie", "buddy", "ruby", "lucky", "bella"]
    var animalImages = ["max", "roxy", "buster", "daisy", "charlie", "maggie", "buddy", "ruby", "lucky", "bella"]


    var section1 = ["max", "roxy", "buster"]
    var section2 = ["daisy","charlie", "maggie"]
    var section3 = ["buddy", "ruby", "lucky"]
    let sections: [String] = ["Section 1", "Section 2", "Section 3"]
    let s1Data: [String] = [item1, item2, item3]
    let s2Data: [String] = [item4, item5, item6]
    let s3Data: [String] = [item7, item8, item9]
    var sectionData: [Int: [String]] = [:]




    override func viewDidLoad() {
        super.viewDidLoad()

        sectionData = [0:s1Data, 1:s2Data, 2:s3Data]
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return sections.count
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
        let imgName = indexPath.row
        cell.animalImage.image = UIImage(named: self.animalImages[imgName])
        cell.animalLabel.text = self.animalLabels[imgName]
        return cell
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
            return sectionInsets
    }


    //I don't think this is doing anything...tried to convert it from tableView code
    func collectionView(collectionView: UICollectionView, titleForHeaderInSection section:Int) -> String? {
        return sections[section]
    }

    override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

        let headerView: Header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: headerViewIdentifier, forIndexPath: indexPath) as! Header

        headerView.headerLabel.text = "Level"

        return headerView

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        print(segue.identifier)
        print(sender)
        if(segue.identifier == "detail"){
            let cell = sender as! CollectionViewCell
            let indexPath = collectionView!.indexPathForCell(cell)
            let vc = segue.destinationViewController as! DetailViewController

            let imgName = animalImages[indexPath!.row]



            print(vc)
            vc.currImage = UIImage(named: imgName)
            vc.textHeading = animalLabels[indexPath!.row]
            //
            //            vc.heading.text = self.titles[0]
            //            vc.imageView.image = UIImage(named: imgName)
        }
    }
}

I am trying to get rid of the sectionData (which has the block of code right above it with let sections, let s1data, and var sections). It is not helping at all. I am trying to replace it instead with

class SectionsData {

func getSectionsFromData() -> [Section] {

    // you could replace the contents of this function with an HTTP GET, a database fetch request,
    // or anything you like, as long as you return an array of Sections this program will
    // function the same way.

    var sectionsArray = [Section]()

    let level1 = Section(title: "Level 1", objects: ["max", "roxy", "buster"])
    let level2 = Section(title: "Level 2", objects: ["daisy","charlie", "maggie"])
    let level3 = Section(title: "Level 3", objects: ["buddy", "ruby", "lucky"])


    sectionsArray.append(level1)
    sectionsArray.append(level2)
    sectionsArray.append(level3)

    return sectionsArray
}

}

and struct Section {

    var heading : String
    var items : [String]

    init(title: String, objects : [String]) {

        heading = title
        items = objects

    }
}

unfortunately I can't call the heading, items, title, or object as a member on the main view page... so I'm stuck at the moment

3 Answers
Related