【问题标题】:Some values showing as disabled in tableview when they shouldn't某些值不应该在 tableview 中显示为禁用
【发布时间】:2020-02-24 16:51:35
【问题描述】:

创建一个简单的货币兑换应用程序,用户可以在其中从列表中选择一种货币,然后从调用的同一个列表中选择另一种货币,然后重定向到比较货币的新视图。在第一次选择之后,我禁用了选择选项,以便无法再次选择它。尽管它正确地禁用了我想要的值,但它也随机禁用了另一个我不知道为什么的 x 量。不知道我编码错了什么,所以任何帮助表示赞赏。执行禁用的函数在cellForRowAt

 class SelectCurrencyTableViewController: UITableViewController {

    var selectedCurrency: String?
    var codesToConvert = [String]()

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

    // Get the JSON data to insert into the table
    func parseJSONData()-> Array<Any> {
        var finalArray = [Any]()
        if let url = Bundle.main.url(forResource: "currencies", withExtension: "json") {
            do {
                let data = try Data(contentsOf: url)
                let jsonResult = try JSONSerialization.jsonObject(with: data)
                if var jsonArray = jsonResult as? [String] {

                    while jsonArray.count > 0 {
                        let result: [String] = Array(jsonArray.prefix(2))
                        finalArray.append(result)
                        jsonArray.removeFirst(2)
                    }
                }
            } catch {
                print(error)
            }
        }
        return finalArray
    }

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

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCellController
        if let array = parseJSONData()[indexPath.row] as? [String]{

            cell.countryCodeLabel.text = array[0]
            cell.currencyLabel.text = array[1]
            cell.countryFlag.image = UIImage(named: array[0])

            // if selected is the same as the current one, disable that row
            if (selectedCurrency == array[0]){
                cell.isUserInteractionEnabled = false
                cell.countryCodeLabel.alpha = 0.3
                cell.currencyLabel.alpha = 0.3
                cell.countryFlag.alpha = 0.3
            }
        }
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if (selectedCurrency != nil){
            if let array = parseJSONData()[indexPath.row] as? [String] {
                codesToConvert.append(selectedCurrency!)
                codesToConvert.append(array[0])
            }
            self.performSegue(withIdentifier: "viewConversions", sender: self)

        } else {
            let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "tableView") as! SelectCurrencyTableViewController
            vc.modalPresentationStyle = .overCurrentContext

            if let array = parseJSONData()[indexPath.row] as? [String]{
                vc.selectedCurrency = array[0]
                show(vc, sender: self)
            }
        }
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60.0
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "viewConversions" {
            let VCTV = segue.destination as! ViewConversionsTableViewController
            VCTV.codesToConvert = codesToConvert
        }
    }
}

【问题讨论】:

    标签: arrays swift uitableview


    【解决方案1】:

    请注意,表格视图单元格是重复使用的,因此当您将didSelectRowAt 中的单元格出列时,您获得的单元格可能是表格视图中的旧单元格之一。表格视图是这样工作的,因此如果您的表格视图真的很长,则不会创建很多 UITableViewCell 对象。

    如果您碰巧将一个已禁用的旧单元出列,则该单元将保持禁用状态,这就是导致意外行为的原因。要解决此问题,您只需在应该启用时将单元格设置为启用状态:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCellController
        if let array = parseJSONData()[indexPath.row] as? [String]{
    
            cell.countryCodeLabel.text = array[0]
            cell.currencyLabel.text = array[1]
            cell.countryFlag.image = UIImage(named: array[0])
    
            // if selected is the same as the current one, disable that row
            if (selectedCurrency == array[0]){
                cell.isUserInteractionEnabled = false
                cell.countryCodeLabel.alpha = 0.3
                cell.currencyLabel.alpha = 0.3
                cell.countryFlag.alpha = 0.3
            } else {
                cell.isUserInteractionEnabled = true
                cell.countryCodeLabel.alpha = 1
                cell.currencyLabel.alpha = 1
                cell.countryFlag.alpha = 1
            }
        }
        return cell
    }
    

    我还建议您不要在表格视图数据源方法中调用parseJSONData。您可以在viewDidLoad 中调用一次,将其存储在一个属性中,然后读取该属性。您无需在每次需要新单元格时重新解析 JSON 数据。

    【讨论】:

    • 感谢您的解释!
    【解决方案2】:
    if (selectedCurrency == array[0]){
        cell.isUserInteractionEnabled = false
        cell.countryCodeLabel.alpha = 0.3
        cell.currencyLabel.alpha = 0.3
        cell.countryFlag.alpha = 0.3
    } else {
        cell.isUserInteractionEnabled = true
        cell.countryCodeLabel.alpha = 1
        cell.currencyLabel.alpha = 1
        cell.countryFlag.alpha = 1
    }
    

    【讨论】:

    • 这工作谢谢!当它允许我时,我会在几分钟内接受作为答案。我不知道为什么它之前失败了,为什么需要反过来
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多