Eccles
Eccles

Reputation: 412

Adding sections in Table View to my existing list?

Is it possible to add sections to my existing list? Or can I somehow hardcode it? So that row 3 and 4 is separated by a section, and row 9 and 10 is separated by a section and so on
I tried to add sections, but that was not very successful:

List File:

import Foundation

class ListItem {
    var section = ""
    var listItem = ""
    var description = ""
    var extraInfo = ""
    var counter: Int = 0

    init(section: String, listItem: String, description: String, ekstraInfo: String) {
        self.section = section
        self.listItem = listItem
        self. description = description
        self.ekstraInfo = ekstraInfo
    }
}    

View Controller:

 let staticList: [ListItem] =
    [

        ListItem(section: "Section 1", listItem: "Bananas", description: "Yellow", ekstraInfo: "Bent"),
        ListItem(section: "Section 2", listItem: "Apples", description: "Red", ekstraInfo: "Round"),
        ListItem(section: "Section 3", listItem: "Strawberries", description: "Red", ekstraInfo: ""),
        ListItem(section: "Section 4", listItem: "Carrots", description: "Orange", ekstraInfo: ""),
        ListItem(section: "Section 5", listItem: "Lime", description: "Green", ekstraInfo: "Round"),
    ]    

 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
    {
        if (tableView == MainTableView)
        {
            return staticList[section].section
        }else
        {
            return  nil
        }
    }    

EDIT:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell: UITableViewCell
            cell = tableView.dequeueReusableCell(withIdentifier: "MenuCell", for: indexPath)

            if let customCell = cell as? MenuCell
            {
                let itemIndex = indexPath.row
                let listItem = staticList[itemIndex]

                customCell.itemLabel.text = listItem.listItem
                customCell.descriptionLabel.text = listItem.description
                customCell.exstraInfoLabel.text = listItem.exstraInfo
                customCell.counterLabel.text = "\(listItem.counter)"

                customCell.delegate = self


            }
            return cell

        }
    }

Upvotes: 0

Views: 1150

Answers (1)

NSAdi
NSAdi

Reputation: 1253

I'll share an example with some hardcoded sections. This should help you understand how it works.

let numberOfRows = [2, 3, 1, 4, 5]

Here we have an array of Integers that indicates the number of rows. Basically 5 sections with 2, 3...5 rows in each section respectively

Add the following to your UITableViewDataSource :

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

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

This should give you a UITableView with 5 sections having 2, 3, 1, 4, 5 rows in each section respectively.

Play around with numberOfRows to get more sections, more rows, etc.

EDIT :

The reason each section loads the same cells is because staticList is a single dimensional array. Hence in each sections the same rows keep getting fetched as indexPath.row starts from 0 for each section. To fix this make the staticList a two dimensional array. Here's how...

let staticList: [[ListItem]] = [
    [
        ListItem(section: "Section 1", listItem: "Bananas", description: "Yellow", ekstraInfo: "Bent"),
        ListItem(section: "Section 1", listItem: "Apples", description: "Red", ekstraInfo: "Round")
    ],
    [
        ListItem(section: "Section 2", listItem: "Strawberries", description: "Red", ekstraInfo: "")
    ],
    [
        ListItem(section: "Section 3", listItem: "Carrots", description: "Orange", ekstraInfo: ""),
        ListItem(section: "Section 3", listItem: "Lime", description: "Green", ekstraInfo: "Round")
    ]
]

Now staticList has 3 sections with 2, 1, 2 ListItems respectively in each section.

Lastly, make a small change in cellForRowAtIndexPath function...

// let itemIndex = indexPath.row
// let listItem = staticList[itemIndex]

let listItem = staticList[indexPath.section][indexPath.row]

Btw, you can remove the section property from ListItem to make things cleaner. Leaving it shouldn't break anything though.

Upvotes: 1

Related Questions