Ios UITableView未正确显示Swift 3中索引的数据

Ios UITableView未正确显示Swift 3中索引的数据,ios,arrays,swift,uitableview,swift3,Ios,Arrays,Swift,Uitableview,Swift3,我的应用程序项目中有一个UITableView,它从数组中提取数据。此外,还必须为这些数据编制索引。但由于某些原因,数据没有显示在适当的部分,如A、B等。我似乎看不出有什么问题。到目前为止,我的数据是: import Foundation import UIKit class uniVC: UITableViewController { let university = ["Allcat University", "Allday University", "Bejnamin Uni

我的应用程序项目中有一个UITableView,它从数组中提取数据。此外,还必须为这些数据编制索引。但由于某些原因,数据没有显示在适当的部分,如A、B等。我似乎看不出有什么问题。到目前为止,我的数据是:

import Foundation
import UIKit

class uniVC: UITableViewController {


    let university = ["Allcat University", "Allday University", "Bejnamin University", "Cat University"]

    var universitySections = [String]()
    var wordsDict = [String:[String]]()

    let wordIndexTitles = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

    func generateWordsDict() {

        for word in university {

            let key = "\(word[word.startIndex])"
            let lower = key.lowercased()

            if var wordValues = wordsDict[lower] {
                wordValues.append(word)
                wordsDict[lower] = wordValues
            } else {
                wordsDict[lower] = [word]
            }

        }

        universitySections = [String](wordsDict.keys)
        universitySections = universitySections.sorted()

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        generateWordsDict()


    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()
        //Dispose
    }

    // Mark table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return universitySections.count
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return universitySections[section]
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let wordKey = universitySections[section]
        if let wordValues = wordsDict[wordKey] {
            return wordValues.count
        }

        return 0
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        let wordKey = universitySections[indexPath.section]
        if wordsDict[wordKey.lowercased()] != nil {

            cell.textLabel?.text = university[indexPath.row]

        }

        return cell
    }

    override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return wordIndexTitles
    }

    override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {

        guard let index = universitySections.index(of: title) else {
            return -1
        }

        return index

    }
}

关于这个有什么想法吗

一切都很好,除了:

if wordsDict[wordKey.lowercased()] != nil {
    cell.textLabel?.text = university[indexPath.row]
}
我认为你犯了一个错误。换成:

if let wordValues = wordsDict[wordKey] {
    cell.textLabel?.text = wordValues[indexPath.row]
}
结果是: 您的问题在于:

if wordsDict[wordKey.lowercased()] != nil {
    cell.textLabel?.text = university[indexPath.row]
}
无论你在哪个部门(即a、b或c),你都会要求第一所大学,而不是该部门的第一所大学。您可能是想查看
单词DICT
,而不是
大学

尝试将其替换为以下内容:

    if let wordValues = wordsDict[wordKey.lowercased()] {
        cell.textLabel?.text = wordValues[indexPath.row]
    }

我不明白你的问题。当前输出和预期输出是什么?不要让人们猜测问题出在哪里。另外,您是否使用断点和调试器来逐步检查代码以查找逻辑错误?我不是让人们猜测问题是什么,阅读问题-数据未显示在相应部分,即a未显示在etc中…添加到的屏幕截图help@sole仅仅因为你认为你已经提供了足够的信息,并不意味着你真的有:)我不得不在操场上运行它,看看哪里出了问题。当你说“数据没有显示在适当的部分”时,这可能意味着很多事情——一个更清楚的解释问题的方法是“以“a”开头的大学出现在每个部分,而我预期“b”部分包含以“b”开头的大学”。使用堆栈溢出的一个副作用是,我了解到提问确实很难:|