Sole
Sole

Reputation: 3340

UITableView not displaying data correctly with Index in Swift 3

enter image description hereI have a UITableView in my app project which pulls data from a Array. Also this data has to be indexed. But for some reason the data does not display in the appropriate sections i.e A, B etc. I can't seem to see whats wrong. My data so far is:

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

    }
}

Any ideas on this one?

Upvotes: 1

Views: 309

Answers (2)

Danh Huynh
Danh Huynh

Reputation: 2327

All of things were fine, excepted:

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

I think you made a mistake. Let change to:

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

This is the result: enter image description here

Upvotes: 1

deanWombourne
deanWombourne

Reputation: 38475

Your problem is here:

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

No matter which section you are in (i.e. a, b or c) you're then asking for the first university, not the first university in that section. You probably meant to look in wordsDict, instead of university.

Try replacing it with this:

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

Upvotes: 1

Related Questions