How can I group TableView items from a dictionary in swift?

后端 未结 7 1457
灰色年华
灰色年华 2020-12-12 16:44

Lets consider this example:

import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
    @IBOutlet weak var tableV         


        
7条回答
  •  长情又很酷
    2020-12-12 17:15

    All collection types must be Array

    var names = [["Tomato", "Potato", "Lettuce"], ["Apple", "Banana"]]
    var sectionNames = ["Vegetables", "Fruits"]
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
      return names[section].count
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int{
      return names.count
    }
    
    func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!{
    
      return sectionNames
    }
    
    func tableView(tableView: UITableView,
      titleForHeaderInSection section: Int) -> String?{
        return sectionNames[section]
    }
    

提交回复
热议问题