How to separate each string in a table view array into its own cell?

470 views Asked by At

I am building an app using swift and have created an array with over 700 rows displayed in the table view. The issue is that I need each row to occupy its own individual cell, but all 700 text strings are grouped to one table view cell. How do I go about separating each string to its own cell without manual creating and labeling 700+ new tableviewcells?

EDIT: Thanks for the help! It seems I really didn't understand how table views worked. I've now made custom tableviewcells and should get the result I'm looking for this way.

1

There are 1 answers

2
Mark On

Assume you have your array let myData = ["item 1", "item 2", "item 3"]

Make sure your UIViewController inherits UITableViewDataSource

Override the following methods and modify accordingly

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Should ideally get UITableViewCell using dequeueReusableCellWithIdentifier
    var cell = UITableViewCell()
    cell.textLabel?.text = myData[indexPath.row]
    return cell
}