0

I'm developing an iOS app and I have a UITableView that generates UITableViewCells based on an array.

In other words, I have a custom cell, which has UIViews inside of it. How can I add a 2dimensional array which contains a Repo Label text and a URL Label text and generate a lot of cells with array.count.

My custom UITableViewCell

Here's my code:

class SourcesViewController: UITableViewController {

    var array:[[String]] = [["Thunderbolt iOS Utilities", "https://repo.thunderbolt.semiak.dev"], ["Semiak's Repo", "repo.semiak.dev"]]

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        //How can I setup all the arguments my custom UITableViewCell needs?
        return cell
    }

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


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

Sorry if the explanation is really bad, I have never done this thing before, moreover, I'm new into iOS development so I do not know how to explain exactly what I need.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
amodrono
  • 1,900
  • 4
  • 24
  • 45

1 Answers1

1

Here's a starting point (I would not recommend this code for production. There are much better ways to do it (e.g., using an array of structs, putting code in the custom cell, etc.))

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    let dataArray = array[indexPath.row] // get the data for the cell
    // not a great idea but....
    cell.repoLabel.text = dataArray.first ?? ""
    cell.urlLabel.text = dataArray.last ?? ""
    return cell
}
AgRizzo
  • 5,261
  • 1
  • 13
  • 28
  • ok, thanks for answering. I already thought about this, but how can I access the `repoLabel` and `urlLabel` without using outlets, as the label is duplicated every cell? – amodrono Jun 05 '19 at 17:43
  • @iAlex11 - For all practical purposes - you cannot access those labels without IBOutlets. What exactly are your concerns? I recommend not thinking that the _label is duplicated for every cell_. The UITableViewCell is a class. Within that class, it has a property that is a UILabel. The dequeueReusableCell 'creates' an instance of that class for you to use. In your above example, you have 2 instances of the cell, each with their own UILabel. – AgRizzo Jun 05 '19 at 17:58
  • Yes, I have a custom class for the cell and create instances of it, but when I add the `repoLabel` and `urlLabel` outlets it gives me the following error: `The [x] outlet from the SourcesViewController is invalid. Outlets cannot be connected to repeating content` – amodrono Jun 05 '19 at 18:07
  • The outlet needs to be connected to your custom cell class. Are you trying to connect it to your viewController? [See this SO question regarding your error](https://stackoverflow.com/questions/26561461/outlets-cannot-be-connected-to-repeating-content-ios) – AgRizzo Jun 05 '19 at 18:10
  • @iAlex11 - You're welcome. Please mark this answer as correct – AgRizzo Jun 07 '19 at 13:13