Swift 如何从字典中实现numberofrowsinsection
我想从字典中创建一个表视图。字典“威士忌”是由“威士忌制造者”的数据制成的,这是一个仅包含语义属性的类。属性“talisker”和“benRiach”仅作为示例。现在一切都正常了,但我不知道如何实现这两个函数Swift 如何从字典中实现numberofrowsinsection,swift,row,cell,Swift,Row,Cell,我想从字典中创建一个表视图。字典“威士忌”是由“威士忌制造者”的数据制成的,这是一个仅包含语义属性的类。属性“talisker”和“benRiach”仅作为示例。现在一切都正常了,但我不知道如何实现这两个函数numberofrowsinssection和cellforrowatinexpath。我想使用威士忌词典中的数据,制作一个tableView,它使用第一个字母作为节头,字典的值作为节中的行。节头工作正常,但我不知道这些行有什么问题,但我无法使它工作。有人能帮我吗 import UIKit
numberofrowsinssection
和cellforrowatinexpath
。我想使用威士忌词典中的数据,制作一个tableView
,它使用第一个字母作为节头,字典的值作为节中的行。节头工作正常,但我不知道这些行有什么问题,但我无法使它工作。有人能帮我吗
import UIKit
class WhiskyOverViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var whiskyArray = [WhiskyBuilder]()
var stringArray: [String] {
return whiskyArray.map{$0.whiskyName!}
}
var whiskies = [Character: [String]]()
var talisker = WhiskyBuilder(name: "Talisker", age: "20", distillery: "Talisk Spring", designation: "Sauterne Cask", volume: "41", town: "Talisk", rating: "9", price: "95", specification: "Good smoky flavour")
var benRiach = WhiskyBuilder(name: "Ben Riach", age: "12", distillery: "Riach", designation: "Cherry finish", volume: "42", town: "Riach", rating: "8", price: "92", specification: "Smoky")
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
whiskyArray.append(talisker)
whiskyArray.append(benRiach)
tableView.rowHeight = 70
tableView.reloadData()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
for entry in stringArray {
if whiskies[entry[entry.startIndex]] == nil {
whiskies[entry[entry.startIndex]] = [String]()
}
whiskies[entry[entry.startIndex]]!.append(entry)
}
return whiskies.keys.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let stringArray = whiskyArray.map{$0.whiskyName!}
let initials = stringArray.initials
// Keine Doppelten
let onlyInitials = initials.setValue
let sortedInitials = onlyInitials.sort {$0 < $1}
return sortedInitials[section]
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ProtoCell
let row = indexPath.row
cell.cellMainLabel!.text = whiskyArray[row].whiskyName
cell.cellDetailLabel!.text = whiskyArray[row].whiskyAge! + " years"
cell.cellDesignationLabel!.text = whiskyArray[row].whiskyDesignation
return cell
}
@IBAction func unwindToMainController(segue: UIStoryboardSegue) {
if let scr = segue.sourceViewController as? DataInsertController{
let newWhiskyName = scr.nameTextField!.text
let newWhiskyDesignation = scr.designationTextField!.text
let newWhiskyAge = scr.ageTextField!.text
let newWhiskyDistillery = scr.distilleryTextField!.text
let newWhiskyPrice = scr.priceTextField!.text
let newWhiskyTown = scr.townTextField!.text
let newWhiskyRating = scr.ratingTextField!.text
let newWhiskyVolume = scr.volumeTextField!.text
let newWhiskySpecification = scr.specificationTextView!.text
let newWhisky = WhiskyBuilder(name: newWhiskyName!, age:newWhiskyAge! , distillery:newWhiskyDistillery! , designation: newWhiskyDesignation!, volume: newWhiskyVolume!, town: newWhiskyTown!, rating: newWhiskyRating!, price: newWhiskyPrice!, specification: newWhiskySpecification!)
whiskyArray.append(newWhisky)
tableView.reloadData()
}
}
}
//MARK: - Extensions
extension Array where Element: StringLiteralConvertible {
var initials: [String] {
return map{String(($0 as! String).characters.prefix(1))}
}
}
extension Array where Element: Hashable {
var setValue: Set<Element> {
return Set<Element>(self)
}
}
导入UIKit
类WhiskyOverViewController:UIViewController、UITableViewDataSource、UITableViewDelegate{
@IBVAR表格视图:UITableView!
var whiskyArray=[WhiskyBuilder]()
var stringArray:[字符串]{
返回whiskyArray.map{$0.whiskyName!}
}
var whiskies=[字符:[字符串]]()
var talisker=威士忌制造商(名称:“talisker”,年龄:“20”,酿酒厂:“Talisk Spring”,名称:“Sauterne桶”,卷:“41”,镇:“Talisk”,评级:“9”,价格:“95”,规格:“良好的烟熏味”)
var benRiach=威士忌制造商(名称:“Ben Riach”,年龄:“12”,酿酒厂:“Riach”,名称:“樱桃味”,卷:“42”,镇:“Riach”,评级:“8”,价格:“92”,规格:“烟熏”)
重写func viewDidLoad(){
super.viewDidLoad()
tableView.dataSource=self
tableView.delegate=self
whiskyArray.append(talisker)
whiskyArray.append(benRiach)
tableView.rowHeight=70
tableView.reloadData()
}
func numberOfSectionsInTableView(tableView:UITableView)->Int{
用于字符串数组中的条目{
如果威士忌[entry[entry.startIndex]==nil{
威士忌[entry[entry.startIndex]=[String]()
}
威士忌[entry[entry.startIndex]!。追加(entry)
}
还威士忌。钥匙。计数
}
func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String{
让stringArray=whiskyArray.map{$0.whiskyName!}
让initials=stringArray.initials
//基恩·多佩尔滕
让onlyInitials=initials.setValue
让sortedInitials=onlyInitials.sort{$0<$1}
返回分类初始值[部分]
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
将cell=tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)设为!ProtoCell
让row=indexPath.row
cell.cellMainLabel!.text=whiskyArray[row].whiskyName
cell.cellDetailLabel!.text=whiskyArray[row]。whiskyAge!+“年”
cell.cellsignationLabel!.text=whiskyArray[row]。whiskyDesignation
返回单元
}
@iAction func UnwindToInControl控制器(序列:UIStoryboardSegue){
如果让scr=segue.sourceViewController为?DataInsertController{
让newWhiskyName=scr.nameTextField!.text
让newWhiskyDesignation=scr.designationTextField!.text
让newWhiskyAge=scr.ageTextField!.text
让NewWhistkyDistillery=scr.distilleryTextField!.text
让newWhiskyPrice=scr.priceTextField!.text
让newWhiskyTown=scr.townTextField!.text
让newWhiskyRating=scr.ratingTextField!.text
让newWhiskyVolume=scr.volumeTextField!.text
让newWhiskySpecification=scr.specificationTextView!.text
让newWhisky=WhiskyBuilder(名称:newWhiskyName!、年龄:newWhiskyAge!、酒厂:NewWhiskyDillertery!、名称:newWhiskyDesignation!、数量:newWhiskyVolume!、城镇:newWhiskyTown!、评级:newWhiskyRating!、价格:newWhiskyPrice!、规格:newWhiskySpecification!)
whiskyArray.append(新威士忌)
tableView.reloadData()
}
}
}
//马克:-分机
扩展数组,其中元素:StringLiteralConvertible{
变量首字母:[字符串]{
返回映射{String($0 as!String).characters.prefix(1))}
}
}
扩展数组,其中元素:可哈希{
var setValue:Set{
返回集(自身)
}
}