1

i am trying to create tableview that can change the data inside it depending on the brand clicked before, for example if i click on the Acura brand the tableview will then change the array into Acura array and when the car model in Acura is clicked it will show the car year. the problem with my code below is that, when the car brand model are clicked it chooses both the CARBRAND and the CARMODEL at the same time and it show the CAR YEAR. it seem the tableview just keep on selecting the row that i clicked

from here

enter image description here

then it keep click all the way to the car year enter image description here both car brand and car model already clicked at this point. enter image description here

how do i stopped tableview from clicking twice. class jobtypeViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { var thetitle = ""

    // car make
        var carbrand = ["Acura","Aston_Martin","AUDI","Bentley","BMW","Buick","Cadillac","Chevrolet","Dodge","FIAT","Ford","Genesis","GMC","Honda","Hyundai","Infinit","Jaguar","JEEP","KIA","Landrover","Lexus","Lincoln","Mazda","MercedezBenz","MINI","Mitsubishi","Nissan","Porsche","Scion","Subaru","Suzuki","Toyota","Volkswagen","Volvo"]

        // car model
        var Acura = ["ILX","MDX","NSX","RDX","RLX","RLX Sport Hybrid","TLX"]
        // car year
        var caryear = ["1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012","2013","2014","2015","2016","2017"]

       func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            switch thetitle {
            case "Acura":
                return Acura.count
            case "brand":
                return carbrand.count
            case "model":
                return Honda.count
            case "year":
                return caryear.count
            default:
                return 0
            }
        }
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
            switch thetitle {
            case "Acura":
                cell.textLabel?.text = Acura[indexPath.row]
                cell.textLabel?.textAlignment = .Center

            case "brand":
                cell.textLabel?.text = carbrand[indexPath.row]
                cell.textLabel?.textAlignment = .Center

            case "model":
                cell.textLabel?.text = Honda[indexPath.row]
                cell.textLabel?.textAlignment = .Center

            case "year":
                cell.textLabel?.text = caryear[indexPath.row]
                cell.textLabel?.textAlignment = .Center

            default:
                cell.textLabel?.text = ""
            }

            return cell
        }


        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            if thetitle == "automotive"{
                switch carbrand[indexPath.row]{
                case "Acura":
                    print(carbrand[indexPath.row])
                    tableviewz.hidden = true
                    thetitle = "Acura"
                    tableviewz.deselectRowAtIndexPath(tableviewz.indexPathForSelectedRow!, animated: true)
                    tableviewz.reloadData()
                default:
                    print("hello")
                }
            }

            if thetitle == "Acura"{
                switch Acura[indexPath.row]{
                case "ILX":
                    print(Acura[indexPath.row])
                    tableviewz.hidden = true
                    thetitle = "year"
                    tableviewz.reloadData()
                    tableviewz.hidden = false
                default:
                    print("hello")
                }
            }
}

1 Answer 1

1

In your didSelectRowAtIndexPath method, when the first if statement is executed, it changes theTitle to "Acura". Then when the next if statement is executed, its condition (thetitle == "Acura") is true and the corresponding statements are executed. To fix it, use else if instead of if for the second if statement: it will then only be executed if the first if condition is false.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if thetitle == "automotive"{
        switch carbrand[indexPath.row]{
            case "Acura":
                print(carbrand[indexPath.row])
                tableviewz.hidden = true
                thetitle = "Acura"
                tableviewz.deselectRowAtIndexPath(tableviewz.indexPathForSelectedRow!, animated: true)
                tableviewz.reloadData()
            default:
                print("hello")
        }
    } else if thetitle == "Acura" {
        switch Acura[indexPath.row]{
            case "ILX":
                print(Acura[indexPath.row])
                tableviewz.hidden = true
                thetitle = "year"
                tableviewz.reloadData()
                tableviewz.hidden = false
            default:
                print("hello")
        }
    }
}

Alternatively restructure the two ifs as a switch/case block.

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

2 Comments

i copied the code and somehow it return blank after i click acura.
@Alan Probably because you have tableviewz.hidden = true.

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.