1

I'm trying to make a table view with sections. So far so good. But when I add the override ViewDidLoad immediately under the structure, I get an error message: Type 'ViewController' does not conform to protocol 'UITableViewDataSource'. If I delete the UITableViewDataSource, everything goes bananas...

Why?

(My next checkpoint is to edit this sections by clicking on each row)

This is my code

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

// Creating a structure
struct Objects {
    var sectionName : String!
    var sectionObjects : [String]!
}

/I would insert viewDidLoad here/

var objectsArray =  [Objects(sectionName: "Section 01", sectionObjects: ["11", "12", "13", "14", "15"]),
                    Objects(sectionName: "Section 02", sectionObjects: ["21", "22", "23"]),
                    Objects(sectionName: "Section03", sectionObjects: ["31"]),
                    Objects(sectionName: "Section04", sectionObjects: ["41", "42", "43","44"])
                    ]

/// Determine the content of the cell: in this case the objectsArray
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")

    cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]

    return cell

}

/// Determine the number of rows
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return objectsArray[section].sectionObjects.count

}

/// Determine the number of sections
func numberOfSections(in tableView: UITableView) -> Int {

    return objectsArray.count

}

/// Determine the Title of the sections

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    return objectsArray[section].sectionName

}

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

}

0 Answers0