如何从字典中实现numberofrowsinsection

时间:2016-07-29 09:30:17

标签: swift row cell

我想从字典中创建一个表格视图。词典“威士忌”是由“WhiskyBuilder”的数据制作的,这是一类只有意义的属性。 “talisker”和“benRiach”这些属性仅作为示例。现在一切正常,但我不知道如何实现两个函数numberOfRowsInSectioncellForRowAtIndexPath。我想使用来自whiskeys-Dictionary的数据,并制作一个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)
    }
}

0 个答案:

没有答案