0

Currently, I am trying to make it so when a cell in a table view is clicked on it adds to a variable. Although, when I print the variable when running the app it just prints 10 over again no matter how many times I click a cell.

Here is my code for the second tableView{

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        //Used to track the number of times the the second tableView was tapped

        if (tableView == secondTable){
            //Function to disable the timer from being used if timer is running
            if isTimerRunning == true {

            }else{

                var tableView2Taps = 0

            //Transfer data to variable to the timer
             selectedindex = indexPath.row

            if (selectedindex == 0){
                seconds = 3600
                secondsreset = 3600
                tableView2Taps += 10

            }
                if (selectedindex == 1){
                seconds = 2700
                secondsreset = 2700
                tableView2Taps += 10
            }
                if (selectedindex == 2){
                seconds = 1800
                secondsreset = 1800
                tableView2Taps += 10
            }
                if (selectedindex == 3){
                seconds = 1200
                secondsreset = 1200
                tableView2Taps += 10
            }
                if (selectedindex == 4){
                seconds = 600
                secondsreset = 600
                tableView2Taps += 10

            }
                print(tableView2Taps)


            }

            //Prevents the user from changing settings when the timer is running
            if isTimerRunning == false{

        if  tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark{

            tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none

        }else{
            tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark}
            }

        }

2 Answers 2

1

You are having a problem of scopes.. You are declaring tableView2Taps every time the user selects a row.

You need to declare the tableView2Taps variable outside the func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) method.

Like this:

var tableView2Taps = 0

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
...
}

Doing this, tableView2Taps becomes an instance variable and the value will be preserved even after the func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) ends.

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

Comments

1

Looks like you are setting it back to 0 every time a selection occurs. Your problem is with this line here:

var tableView2Taps = 0

Try storing the above as a variable on the view controller instead.

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.