Reputation: 2040
I'm developing an iOS app and I have a UITableView
that generates UITableViewCell
s 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
.
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.
Upvotes: 0
Views: 54
Reputation: 5271
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
}
Upvotes: 1