2017-04-03 63 views
1

enter image description here我的应用程序项目中有一个UITableView,它从数组中提取数据。此数据也必须编入索引。但由于某些原因,数据不会显示在适当的部分,即A,B等。我似乎无法看到什么是错的。我的数据到目前为止是:UITableView无法正确显示索引在Swift 3中的数据

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 

    } 
} 

对此问题的任何建议?

+2

我不明白你的问题。目前的产出是多少?预期的产出是多少?不要让人猜测问题是什么。另外,您是否使用了断点和调试器来遍历代码以查找逻辑中的错误? –

+0

我不是让人猜测问题是什么,阅读问题 - 数据不显示在适当的部分,即一个不显示在等... – Sole

+0

截图添加到帮助 – Sole

回答

1

所有的东西都很好,除外:

if wordsDict[wordKey.lowercased()] != nil { 
    cell.textLabel?.text = university[indexPath.row] 
} 

我想你搞错了。让我们改变:

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

这就是结果: enter image description here

+0

谢谢,工作正常。 – Sole

1

你的问题是在这里:

if wordsDict[wordKey.lowercased()] != nil { 
    cell.textLabel?.text = university[indexPath.row] 
} 

无论你是在哪个部分(即A,B或C)你然后再问第一所大学,而不是第一所大学,该部分是。你可能打算看看wordsDict,而不是university

尝试用这种替代它:

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