samedi 9 juin 2018

'else' statement not running in 'didSelectRowAt' function while executing

don't know whether it is common or not but in 'didSelectRowAt' function, 'if' statement is executing perfectly but 'else' statement is not and whenever i tap on the cell the checked property is always true down below is the code which might help :-

here is the code :-

import UIKit
import Alamofire
import SwiftyJSON

class TableViewController: UITableViewController {

    var vCData  = [Item]()

    let url = "https://api.coinmarketcap.com/v1/ticker/"
    var items = [Item]()

    override func viewDidLoad() {
        super.viewDidLoad()
        getCoinData(url: url)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

        let arrayItem = items[indexPath.row]

        cell.textLabel?.text        = arrayItem.name
        cell.detailTextLabel?.text  = arrayItem.symbol

        if arrayItem.checked == true{cell.accessoryType = .checkmark}
        else{cell.accessoryType = .none}

        return cell
    }

THIS IS WHERE PROBLEM OCCURS :-

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

        tableView.deselectRow(at: indexPath, animated: true)

        var selItem = items[indexPath.row]

        if selItem.checked == false {

            selItem.checked = true

            vCData.append(selItem)

        }else {                       // statement not working

            selItem.checked = false

            if let index : Int = self.vCData.index(where: {$0.name == selItem.name && $0.checked == selItem.checked && $0.buyPrice == selItem.buyPrice && $0.rank == selItem.rank && $0.symbol == selItem.symbol}) {

                self.vCData.remove(at: index)
                print(vCData,"\n")
            }
        }
        tableView.reloadData()
    }

    @IBAction func doneTapped(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true, completion: nil)
    }

}

Aucun commentaire:

Enregistrer un commentaire