0

Ok so I have been scouring this site to find a solution to my problem. I have found one but for some reason by code just won't work the way intended. My goal is to have a table view with sections in it. Under each section is a different amount (such as 1 in one section and 3 cells in another section) of cells in each section.

Here is my code

import UIKit

class cat1: UIViewController, UITableViewDelegate, UITableViewDataSource {

let proc = [["this"], ["Ye", "it", "will"]]

let infor = [["info 1"] , ["info 3", "info 2" , "info 4"]]
let arrayforsec = [ "test", "testtt"]


override func viewDidLoad() {


    super.viewDidLoad()
}

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



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

      return  proc[section].count
           }



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as UITableViewCell
    let cellText = proc[indexPath.section][indexPath.row]
    let descriptiontxt = infor[indexPath.section][indexPath.row]


     cell.detailTextLabel?.numberOfLines = 0
     cell.detailTextLabel?.lineBreakMode = .byWordWrapping

     cell.textLabel?.text = cellText
    cell.detailTextLabel?.text = descriptiontxt

    return (cell)
}

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



      }


func numberOfSectionsInTableView(in tableView: UITableView) -> Int {

    return proc.count
}

}

My goal is to have 2 diffferent headers, test and testtt

test will have the cell with the title this and the subtitle info 1 and so on

What am I messing up?

here is the solution I consulted :How do I populate two sections in a tableview with two different arrays using swift?

(the response w/ 40 upvotes)

  • Are any of your `UITableViewDataSource` or `UITableViewDelegate` methods being called? – AdamPro13 May 02 '17 at 03:20
  • yes I think so, the table only populates the first section and cell so: "test" as section, "this" as description and "info 1" as subtitle, the rest just does not show up... – Joe Macksood May 02 '17 at 03:24
  • @GaryMakin that's not true if he added a `UITableView` as a subview of the view controller rather than use `UITableViewController`. – AdamPro13 May 02 '17 at 03:26
  • @GaryMakin it is a viewcontroller with a tableview dropped in via storyboard – Joe Macksood May 02 '17 at 03:27
  • When I wrote my first comment I was thinking that the delegate wasn't set, which could be if the class was wrong. When I saw that he got one section, I realised what the problem was. – Gary Makin May 02 '17 at 03:28
  • @GaryMakin ok, that makes sense – Joe Macksood May 02 '17 at 03:31

1 Answers1

1

You have the name of a function wrong. It should be:

func numberOfSections(in tableView: UITableView) -> Int {
    return proc.count
}

This was renamed in a version of Swift newer than in the question you were referencing. Because Swift has been changing regularly, it’s worth double checking the names of functions and symbols.

Gary Makin
  • 3,109
  • 1
  • 19
  • 27