Johnny Microseed
Johnny Microseed

Reputation: 29

Variable is not changing when adding in swift

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

        }

Upvotes: 0

Views: 57

Answers (2)

Enrique Bermúdez
Enrique Bermúdez

Reputation: 1760

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.

Upvotes: 1

Eman Harout
Eman Harout

Reputation: 536

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.

Upvotes: 1

Related Questions