JiosDev
JiosDev

Reputation: 324

Fill a table with sorted images

I just started to learn swift and am having some difficulties in this task. I have 15 pictures in the assets also have a table with two sections. Need to fill in the cells with sorted picture. Cell Example (.textLabel = nameImage + image ) The first section should have pictures from the 1st number to the 7th. The second section should be from 8th to 15th.

This needs to be implemented in different files. In one file only structures with properties. In another, filling tables with sorted pictures. My weakness is that I do not know how to fully implement it. I created two structures. But I do not know what to do next

Struct.swift

import Foundation
struct imageOneSection {
    var titleImageOneSec:String
    var numImageOneSec: Int

    init(titleImageOneSec:String, numImageOneSec) {
        self.titleImageOneSec = titleImageOneSec
        self.numImageOneSec = numImageOneSec
    }
}

struct imageTwoSection {
    var titleImageTwoSec:String
    var numImageTwoSec: Int

    init(titleImageTwoSec:String, numImageTwoSec) {
        self.titleImageTwoSec = titleImageTwoSec
        self.numImageTwoSec = numImageTwoSec
    }
}

Upvotes: 0

Views: 97

Answers (1)

Valentina Konatar
Valentina Konatar

Reputation: 277

You can use only one struct and set multidimensional array, where one array would be set of images from 1 to 7 and another from 8 to 15. In the file where your adding content to your cell you can create:

let data: [[imageOneSection]] = [[imageOneSection.init(titleImageOneSec: imageName), numImage), ...], 
                                 [imageOneSection.init(titleImageOneSec: imageName, numImage), ...]]

Then for your table view methods add:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return data.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data[section].count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     // init your cell
     cell.addContent (data[indexPath.section][indexPath.row])
}

In your cell class add:

 func addContent (data: imageOneSection) {
      yourTextLabel.text = data.titleImageOneSec + "\(data. numImageOneSec)"
      yourImageView.image = UIImage.init(named: data.titleImageOneSec)
 }

Hope this will help you.

Upvotes: 3

Related Questions