1

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?

4
  • 2
    I don't understand your question. What is the current output and what is the expected output? Don't make people guess what the problem is. Also, have you used breakpoints and the debugger to step through your code to look for your error in logic? Commented Apr 3, 2017 at 9:52
  • I am not making people guess what the problem is, read the question - the data is not displaying in the appropriate sections i.e. a is not displaying in a etc... Commented Apr 3, 2017 at 9:55
  • screenshot added to help Commented Apr 3, 2017 at 9:56
  • @sole Just because you think you have given enough information, doesn't mean that you actually have :) I had to run it in a playground to see what was wrong. When you say 'data is not displaying in the appropriate sections' that could mean any number of things - a clearer way to explain what was going wrong would be 'Universities beginning with 'a' are appearing in every section, when I expected section 'b' to contain universities beginning with b'. A side effect of using stack overflow is that I've learned that asking questions is really really hard :| Commented Apr 3, 2017 at 10:04

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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]
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.