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

后端 未结 7 1444
灰色年华
灰色年华 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:13

    From Apple Documentation :

    var keys: LazyForwardCollection, Key>> { get }

    Description: A collection containing just the keys of self. Keys appear in the same order as they occur as the .0 member of key-value pairs in self. Each key in the result has a unique value.

    names.keys.array returns an Array of the keys.

    SO:

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return names.keys.array[section].count
    }
    
    func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!{
    return names.keys.array
    }
    
    func tableView(tableView: UITableView,
        titleForHeaderInSection section: Int) -> String?{        
    return names.keys.array[section]
    }
    

    This will work on Any Dictionary with any amount of data(even if it is unknown to the programmer

提交回复
热议问题