简体   繁体   中英

Programatically adding sections and cells to Table View Swift

Say I have a list/array of sections:

let sections = [new Section("today", todaylist), 
                new Section("yesterday", yestlist), 
                new Section("25th February", list25f),...]

As you can see each section has a section name and a list of objects which will populate the cells inside that particular section.

For now, let's say these objects are just simple strings.

How would I programmatically loop through sections and create a new section with the appropriate title and the appropriate number of cells.

Ie- the number of cells for section no. i would be:

sections[i].getList().count

in the case of "today" that would be equivalent to

todaylist.count

I can't add the sections in the storyboard because it will vary, the table view will be dynamic !

Thanks for any help !

Check out this code:

import UIKit

class TableViewController: UITableViewController {

    var names = ["Vegetables": ["Tomato", "Potato", "Lettuce"], "Fruits": ["Apple", "Banana"]]

    struct Objects {

        var sectionName : String!
        var sectionObjects : [String]!
    }

    var objectArray = [Objects]()

    override func viewDidLoad() {
        super.viewDidLoad()

        for (key, value) in names {
            println("\(key) -> \(value)")
            objectArray.append(Objects(sectionName: key, sectionObjects: value))
        }
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return objectArray.count
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return objectArray[section].sectionObjects.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

        // Configure the cell...
        cell.textLabel?.text = objectArray[indexPath.section].sectionObjects[indexPath.row]
        return cell
    }

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

        return objectArray[section].sectionName
    }
}

Hope it will help you.

Reference from my old answer.

You can do this by using a dictionary, since you are working on strings only, it may be simple.

eg.

let sections : [String: [String]] = [
  "Today": ["list1", "list2", "list3"],
  "Yesterday": ["list3", "list4", "list5"]
  // and continue
]

and sections use this:

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

  return sections.count
}

for number of cells inside a section, you can create another array with section titles

let days = ["Today", "Yesterday", "SomeOtherDays"]

and in numberOfRowsInSection:

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

  let dayKey = days[section]

  if let daylist = sections[dayKey] {
      return daylist.count
  } else {
      return 0
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM