-1

My Scenario, I am trying to create UITableView with custom cell and multiple sections from single array. Here, I need to show different titles in sections. How to do this? I used below code but not able to get clear understanding.

My Code below

var  data = [["1","2","3"], ["4","5"]]
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return data.count
}

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 20
} 
Ben Dev
  • 77
  • 1
  • 12
  • 1
    you should implement UITableViewDelegate and UITableViewDataSource methods – Alexandr Kolesnik Aug 01 '19 at 09:12
  • Possible duplicate of [UITableView get titleForHeadersInSection swift](https://stackoverflow.com/questions/27011131/uitableview-get-titleforheadersinsection-swift) – dahiya_boy Aug 01 '19 at 09:12
  • @dahiya_boy My scenario I am asking single array to add both data sections and row – Ben Dev Aug 01 '19 at 09:26
  • What should be the title for each section? – PGDev Aug 01 '19 at 09:26
  • @ PGDev I need to give some string titles. as of now you can provide some sample data. In future I am going to load JSON data into that. As of now mock data enough – Ben Dev Aug 01 '19 at 09:28

1 Answers1

3

There can be 2 cases around it,

  1. In case you simply want to add String titles to each section, implement UITableViewDataSource's tableView(_: titleForHeaderInSection:) method.

  2. And if you want to give a custom view for each section, implement UITableViewDelegate's tableView(_:viewForHeaderInSection:) method.

Here is an example with tableView(_: titleForHeaderInSection:),

class VC: UIViewController, UITableViewDataSource {
    let data = [["1","2","3"], ["4","5"]]
    let sectionNames = ["This is Sec-1", "And this is Sec-2"]

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data[section].count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
        cell.textLabel?.text = data[indexPath.section][indexPath.row]
        return cell
    }

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

Do implement tableView(_:heightForHeaderInSection:) in case you need custom height for each section.

Screenshot:

enter image description here

Edit:

Use accessoryType as .checkmark for cell selection.

Create a custom UITableViewCell and override setSelected(_:animated:) method in that like so,

class CustomCell: UITableViewCell {
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.accessoryType = selected ? .checkmark : .none
    }
}

Set the reuseIdentifier of CustomCell in xib as cell. Also, update the tableView(_:cellForRowAt:) method to dequeue CustomCell instance. I updated that in the code above.

PGDev
  • 23,751
  • 6
  • 34
  • 88
  • Superw. Here I have one doubt If I want to load custom data into section. how to do that? within same array instead of count how to fetch custom data? – Ben Dev Aug 01 '19 at 09:40
  • 1
    Fetch the custom section name using section parameter in titleForHeaderInSection method. I've updated the answer to show you can example. – PGDev Aug 01 '19 at 09:44
  • I am sorry Can you please add check mark into your answer. Checkmark should be select one cell at a time. – Ben Dev Aug 01 '19 at 10:40
  • That's a separate requirement. You should ask a separate question for that. – PGDev Aug 01 '19 at 10:41
  • Updated the answer. Enjoy.. – PGDev Aug 01 '19 at 10:49