2016-11-09 79 views
-1

我想在我的TableView中添加部分,如何使用swift3

我必须让我的TableView内部分:存储在“节”

var sections = SectionData().getSectionsFromData() // Declaration: [(key: String, value: [String])] 

我所有的数据。在存储所有的ABC钥匙,和值所有以26个字母的一个启动的项目相应

我无法弄清楚如何访问值

我的代码:

var sections = SectionData().getSectionsFromData() 

override func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return sections.count 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return sections.[section].count // error 
} 

回答

0

我假设你的数据源是元组的一个这样的数组:

let sections: [(key: String, value: [String])] = [("A", ["Andrew", "Anna"]), ("B", ["Barbie", "Brad"])] 

然后你numberOfRowsInSection方法应该是这样的:

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

部分是[字符串:[字符串]]的字典,并且您试图使用整数部分将其索引。相反,您必须通过该部分对已排序的键进行索引,然后使用正确的键来索引部分词典。

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    let keys = sections.keys.sorted() 
    let key = keys[section] 
    guard let rows = sections[key] else { 
     return 0 
    } 
    return rows.count 
}