【问题标题】:UISegmented Control not working properlyUISegmented 控件无法正常工作
【发布时间】:2017-12-27 20:54:25
【问题描述】:

我有一个如下所示的分段控件:

在 cellForRowAt 中使用以下代码:

switch cell.methodType.selectedSegmentIndex{
            case 0: expense.cash = true && expense.credit != true
            case 1: expense.credit = true && expense.cash != true
            default:
                break
            }

当我单击其他 3 列时,我选择了一个选项,然后我将被定向回该视图控制器,该视图控制器如下所示。

但是,在随机时间,分段控制可以从信用切换到现金,我必须重新选择信用。我不知道为什么会这样。它有时会发生,而其他时候不会发生。每当发生这种情况时,我的费用类型就会从贷记转换为现金,并且不会注册为贷记。我在另一个视图控制器中的 cellForRowAt 中有以下代码,其中所有费用都得到保存,如下所示:

 if expense.cash && expense.expense{
        print("cash")
        cell.cashOrCredit.image = #imageLiteral(resourceName: "Cash-Expense Icon")
    }
    else if expense.cash && expense.income{
        print("cash")
        cell.cashOrCredit.image = #imageLiteral(resourceName: "Cash-Income Icon")
    }
    else if expense.credit && expense.income{
        print("credit")
        cell.cashOrCredit.image = #imageLiteral(resourceName: "Credit-Income Icon")
    }
    else if expense.credit && expense.income{
        print("credit")
        cell.cashOrCredit.image = #imageLiteral(resourceName: "Credit-Expense Icon")
    }

如果在我单击其他 3 行时分段控件没有更改,则它注册为“信用”,但如果它随机决定更改为现金,则费用注册为“现金”。

这是我整个 cellForAtIndexPath 的代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "listCell", for: indexPath) as! ListDataTableViewCell

    if indexPath.row == 0{
        cell.dataTitleLabel.text = "Category"
        cell.methodType.isHidden = true
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        cell.optionSelectedLabel.isHidden = false
        cell.optionSelectedLabel.text! = categoryDisplayed
        cell.optionSelectedLabel.adjustsFontSizeToFitWidth = true

    } else if indexPath.row == 1{
        cell.dataTitleLabel.text = "Method"
        cell.methodType.isHidden = false
        cell.methodType.tintColor = UIColor(red:0.29, green:0.68, blue:0.71, alpha:1.0)
        cell.optionSelectedLabel.isHidden = true
        cell.accessoryType = .none
        cell.selectionStyle = .none

    } else if indexPath.row == 2{
        cell.dataTitleLabel.text = "Currency"
        cell.methodType.isHidden = true
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        cell.optionSelectedLabel.isHidden = false
        cell.optionSelectedLabel.text! = currencyDisplayed
        cell.optionSelectedLabel.adjustsFontSizeToFitWidth = true
        cell.optionSelectedLabel.minimumScaleFactor = 0.2

    } else if indexPath.row == 3{
        cell.dataTitleLabel.text = "Collection"
        cell.methodType.isHidden = true
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        cell.optionSelectedLabel.isHidden = false
        cell.optionSelectedLabel.text! = collectionsDisplayed
        cell.optionSelectedLabel.adjustsFontSizeToFitWidth = true


    }
    return cell
}

我尝试将代码更新为此

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "listCell", for: indexPath) as! ListDataTableViewCell
    let cell2 = tableView.dequeueReusableCell(withIdentifier: "listCell2", for: indexPath) as! ListDataTableViewCell2

    if indexPath.row == 0{
        cell.dataTitleLabel.text = "Category"
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        cell.optionSelectedLabel.isHidden = false
        cell.optionSelectedLabel.text! = categoryDisplayed
        cell.optionSelectedLabel.adjustsFontSizeToFitWidth = true
        return cell

    } else if indexPath.row == 1{
        cell2.dataLabel.text = "Method"
        cell2.methodType.isHidden = false
        cell2.methodType.tintColor = UIColor(red:0.29, green:0.68, blue:0.71, alpha:1.0)
        cell2.accessoryType = .none
        cell2.selectionStyle = .none
        return cell2

    } else if indexPath.row == 2{
        cell.dataTitleLabel.text = "Currency"
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        cell.optionSelectedLabel.isHidden = false
        cell.optionSelectedLabel.text! = currencyDisplayed
        cell.optionSelectedLabel.adjustsFontSizeToFitWidth = true
        cell.optionSelectedLabel.minimumScaleFactor = 0.2
        return cell

    } else if indexPath.row == 3{
        cell.dataTitleLabel.text = "Collection"
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        cell.optionSelectedLabel.isHidden = false
        cell.optionSelectedLabel.text! = collectionsDisplayed
        cell.optionSelectedLabel.adjustsFontSizeToFitWidth = true
        return cell
    }
    else {
        return cell
    }
}

但在声明 cell2 的行中遇到 SIGABRT 错误

【问题讨论】:

  • 当用户点击分段控件时,您如何保存选定状态 - “现金”或“信用”?
  • 在代码的第一位
  • 您应该将选择的索引保存在其他地方(例如在 viewController 本身中),而不是在单元格中。单元格可以重复使用,因此您不能依赖保存在那里的数据。
  • 展示你完整的cellForRowAt 实现。问题就在那里。
  • 这可能是由于细胞重复使用,但我们确实需要查看更多细节。您如何将信息数据与 UI 实现分开。另外,当您说“随机”时,这些“随机”时间实际上在做什么。

标签: ios swift uisegmentedcontrol


【解决方案1】:

分段控件不会随机翻转。我可以告诉你,这发生在你滚动带有段控件的单元格远离视线并再次返回之后。

当单元格从视线中消失时,它们会重新用于使用相同单元格标识符的其他单元格。如果你有一个很长的表,单元格也可能会被释放。

假设您没有很长的表格,我的猜测是您没有专门用于具有段控件的单元格的单元格标识符。如果是这种情况,请尝试为该分段单元格提供唯一标识符;问题应该会消失。

但这还不够。如果它不在视线范围内(足够长/足够远),您的分段单元仍可能会被释放。因此,您需要将段控制选择存储在视图控制器中的某处,并将所选段设置为cellForRowAt。如果您这样做,则不再需要唯一的单元格标识符。

在这里看到您添加的代码有几件事:

  1. 您只能获取cellForRowAt indexPath: 中的两个单元格之一,具体取决于indexPath。所以你可以将dequeueReusableCell 放在if 块内。
  2. 您仍然没有设置methodType 的值(请参阅我上面的回答)。
  3. Type 一词令人困惑。根据相当普遍的命名风格,我称之为methodSegmentControl(或methodSegment)。
  4. 确实,正如您所做的那样,通常会创建两个单元类:ListDataTableViewCellListDataTableViewCell2。您现在可以去掉 cell.methodType.isHidden = true/false,因为只有单元格 2 需要段控制。

我希望这有助于并解决问题。祝你好运!

您似乎缺乏有关如何在 iOS 中使用表格和单元格的基本知识。我建议你通读一个很好的教程。我会为您节省大量时间尝试通过反复试验来解决问题。

【讨论】:

  • 我一定会尝试的!
  • @A.G 那么,这就是解决方案吗?
  • 我尝试实现它(我发布了上面的代码),但我得到一个 SIGABART 错误
  • @A.G 我已对您的问题添加了评论并扩展了我的答案。我希望你能尽快完成所有工作!
  • 我添加了 dequeReusablecell,但仍然有一个 SIGABART :(
猜你喜欢
  • 2014-07-23
  • 2016-01-15
  • 1970-01-01
  • 1970-01-01
  • 2013-07-26
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
  • 1970-01-01
相关资源
最近更新 更多