0
votes

I guess this could be one of my rookie mistakes I couldn't figure out.

I have an app which has a table view. It has text label and detail text label.

When I select a row, I takes me to another story board using segue...all of this works fine except the table view display on my simulator.

detail text label shows up on the simulator shown in this picture circled.

Here is the code I am using to detect cell/row selected. When I comment it out this issue goes away...

What you see in the red circle is gradeselected which is also in the detail text label in the tableview.

func sectionIndexTitles(for tableView: UITableView) -> [String]? {
    let gradeselected = String(describing: sgrade)
    return [gradeselected]
}

Screenshot of simulator with the issue

enter image description here

Please help in resolving this issue. Let me know if you need any more info.

Xcode 9.1 Swift 4

@Caleb here is my code.

import UIKit

class StudentsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var cellButton: UIButton!

    @IBOutlet weak var studentDetailTable: UITableView!
    var sname:[String]?
    var sgrade:[Int]?

    var gradetext = "Grade:"
    var sstudentname = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        studentDetailTable.delegate = self
        studentDetailTable.dataSource = self

        // Do any additional setup after loading the view.
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sname!.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = studentDetailTable.dequeueReusableCell(withIdentifier: "cell")

        cell?.textLabel?.text = sname[indexPath.row] + gradetext + String(sgrade[indexPath.row])
        sstudentname = sname![indexPath.row]


        cell?.detailTextLabel?.text = String(sgrade![indexPath.row])
        cell?.layer.cornerRadius = (cell?.frame.height)!/2
        cell?.backgroundColor = UIColor.blue
        cell?.textLabel?.textColor = UIColor.white
        cell?.layer.borderWidth = 6.0
        cell?.layer.cornerRadius = 15
        cell?.layer.borderColor = UIColor.white.cgColor
        cell?.textLabel?.textColor = UIColor.white

        return cell!
    }



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedIndex = tableView.dataSource?.sectionIndexTitles!(for: studentDetailTable)
        let indexPath = tableView.indexPathForSelectedRow
        let currentCell = tableView.cellForRow(at: indexPath!)!
        let scell = currentCell.detailTextLabel!.text!
        sstudentname = (currentCell.textLabel?.text)!
    }


// - If I comment this section of the code issue goes away.

    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        let gradeselected = String(describing: sgrade)
        return [gradeselected]
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let myKLVC = segue.destination as! KindergartenLevelViewController
        myKLVC.klvstudentname = sstudentname

    }
2
Show your storyboard UI for this Screen. - dahiya_boy
You should use didSelectetRowAtIndexPath a delegate method of IUTableView which tells you which cell you have tapped - Vikky
What is your sgrade variable? Why do you convert it to a String using String(describing)? And then why do you return an array containing just that one string? Note that the sectionIndexTitles has nothing to do with any cell or any selected row. - rmaddy
FWIW, the site tour has a decent overview of major site features that can be helpful. - EJoshuaS - Reinstate Monica

2 Answers

0
votes

The text in the red circle says [1, 2], which looks like the array that probably holds all the grades, not just the one for a specific cell that we see in the string gradeselected. If you have such an array in your code, look for places where you might be converting it to a string and drawing it. Maybe you did that in an earlier iteration of your code to make sure that the array contained what you thought, or something?

Arrays don't just mysteriously draw themselves on the screen — somewhere, there's some code that causes that to happen. We can't really help you find it because you haven't shown very much of your code, but just knowing what to look for may help you find it yourself.

-2
votes

You can query the selected row via table view's property indexPathForSelectedRow.

The method you have implemented does exactly what you see in the simulator.

Just have a look at the documentation:

property indexPathForSelectedRow: https://developer.apple.com/documentation/uikit/uitableview/1615000-indexpathforselectedrow

func sectionIndexTitles: https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614857-sectionindextitles