How to fill a tableView using for loop and Dictionary

Asked

Viewed 92 times

0

I’m making an app that requires several tableviews. Some of them have more than 20 cells and some only 4, 5.

Then I realized that making 20 and so many lines of if Is is a terrible practice. But as I am very new to Swift, I would like to know how I can use a for loop that fills the tableviews with Cells.

Below my code

import UIKit

class StyleStrengthsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var strengthsDictionary : [String:String] = ["ProfilePicture":"profilePictureCell",
                                                 "atitudesBehaviours":"atitudesBehavioursCell",
                                                 "attitude1":"attitude1Cell",
                                                 "attitude2":"attitude2Cell",
                                                 "attitude3":"attitude3Cell",
                                                 "attitude4":"attitude4Cell",
                                                 "attitude5" : "attitude5Cell",
                                                 "Approach":"approachCell",
                                                 "about":"aboutCell" ,
                                                 "skillandAbilities":"skillsAndAbilitiesCell",
                                                 "skill1cell":"skill1Cell",
                                                 "skill2":"skill2Cell",
                                                 "skill3" : "skill3Cell",
                                                 "skill4":"skill4Cell",
                                                 "skill5":"skill5Cell",
                                                 "skill6":"skill6Cell",
                                                 "skill7":"skill7Cell",
                                                 "skill8":"skill8Cell",
                                                 "skill9" : "skill9Cell",
                                                 "skill10" : "skill10Cell",
                                                 "skill11":"skill11Cell",
                                                 "skill12":"skill12Cell"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return strengthsDictionary.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{


        if indexPath.row == 0 {
            let profilePicture = tableView.dequeueReusableCell(withIdentifier: "profilePictureCell")
            tableView.rowHeight = UITableViewAutomaticDimension
            tableView.estimatedRowHeight = 300
            return profilePicture!
        }else if indexPath.row == 1 {
            let attitudeCell = tableView.dequeueReusableCell(withIdentifier: "atitudesBehavioursCell")
            return attitudeCell!
        }else if indexPath.row == 2 {
            let attitude1 = tableView.dequeueReusableCell(withIdentifier: "attitude1Cell")
            return attitude1!
        }else if indexPath.row == 3 {
            let attitude2 = tableView.dequeueReusableCell(withIdentifier: "attitude2Cell")
            return attitude2!
        }else if indexPath.row == 4 {
            let attitude3 = tableView.dequeueReusableCell(withIdentifier: "attitude3Cell")
            return attitude3!
        }else if indexPath.row == 5 {
            let attitude4 = tableView.dequeueReusableCell(withIdentifier: "attitude4Cell")
            return attitude4!
        }else if indexPath.row == 6 {
            let attitude5 = tableView.dequeueReusableCell(withIdentifier: "attitude5Cell")
            return attitude5!
        }else if indexPath.row == 7 {
            let approach = tableView.dequeueReusableCell(withIdentifier: "approachCell")
            return approach!
        }else if indexPath.row == 8 {
            let about = tableView.dequeueReusableCell(withIdentifier: "aboutCell")
            return about!
        }else if indexPath.row == 9 {
            let skillAndAbilities = tableView.dequeueReusableCell(withIdentifier: "skillsAndAbilitiesCell")
            return skillAndAbilities!
        }else if indexPath.row == 10 {
            let skill1 = tableView.dequeueReusableCell(withIdentifier: "skill1Cell")
            return skill1!
        }else if indexPath.row == 11 {
            let skill2 = tableView.dequeueReusableCell(withIdentifier: "skill2Cell")
            return skill2!
        }else if indexPath.row == 12 {
            let skill3 = tableView.dequeueReusableCell(withIdentifier: "skill3Cell")
            return skill3!
        }else if indexPath.row == 13 {
            let skill4 = tableView.dequeueReusableCell(withIdentifier: "skill4Cell")
            return skill4!
        }else if indexPath.row == 14 {
            let skill5 = tableView.dequeueReusableCell(withIdentifier: "skill5Cell")
            return skill5!
        }else if indexPath.row == 15 {
            let skill6 = tableView.dequeueReusableCell(withIdentifier: "skill6Cell")
            return skill6!
        }else if indexPath.row == 17 {
            let skill7 = tableView.dequeueReusableCell(withIdentifier: "skill7Cell")
            return skill7!
        }else if indexPath.row == 18 {
            let skill8 = tableView.dequeueReusableCell(withIdentifier: "skill8Cell")
            return skill8!
        }else if indexPath.row == 19 {
            let skill9 = tableView.dequeueReusableCell(withIdentifier: "skill9Cell")
            return skill9!
        }else if indexPath.row == 20 {
            let skill10 = tableView.dequeueReusableCell(withIdentifier: "skill10Cell")
            return skill10!
        }else if indexPath.row == 21 {
            let skill11 = tableView.dequeueReusableCell(withIdentifier: "skill11Cell")
            return skill11!
        }else {
            let skill12 = tableView.dequeueReusableCell(withIdentifier: "skill12Cell")
            return skill12!
        }
    }


}

1 answer

1


The method dequeueReusableCell(withIdentifier:) of UITableView serves to reuse cells, so each identifier will load a different cell. This is used to differentiate cells with layouts different.

If they all have the same layout - just changing the content - you can turn this dictionary into an array:

var strengths: [String] = ["ProfilePicture",
                           "atitudesBehaviours",
                           "attitude1",
                           "attitude2",
                           "attitude3",
                           "attitude4",
                           "attitude5",
                           "Approach",
                           "about",
                           "skillandAbilities",
                           "skill1cell",
                           "skill2",
                           "skill3",
                           "skill4",
                           "skill5",
                           "skill6",
                           "skill7",
                           "skill8",
                           "skill9",
                           "skill10",
                           "skill11",
                           "skill12"]

And use index to fill Tableview:

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "cellIdentifier"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 300

    // ajustes de conteúdo, ex:
    cell?.textLabel?.text = strengths[indexPath.row]

    return cell
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.