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.

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

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?
@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.
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
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
@iAlex11 - You're welcome. Please mark this answer as correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.