Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UISegmented控件工作不正常_Ios_Swift_Uisegmentedcontrol - Fatal编程技术网

Ios UISegmented控件工作不正常

Ios UISegmented控件工作不正常,ios,swift,uisegmentedcontrol,Ios,Swift,Uisegmentedcontrol,我有一个分段控件,如下所示: 在cellForRowAt中使用以下代码: switch cell.methodType.selectedSegmentIndex{ case 0: expense.cash = true && expense.credit != true case 1: expense.credit = true && expense.cash != true default

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

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

但是我遇到了一个SIGABRT错误,它声明了cell2,分段控制不随机翻转。我可以告诉你,这发生在你用段控制滚动单元格到足够远的地方,然后再滚动回来之后

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

假设您没有很长的表,我的猜测是您没有一个专用的单元标识符,仅用于具有段控件的单元。如果是这样的话,试着给分割的单元格一个唯一的标识符;这个问题应该消失

但这还不够。如果您的分段单元看不见(足够长/足够远),它仍可能被解除分配。因此,您需要将段控件选择存储在视图控制器中的某个位置,并在
cellForRowAt
中设置选定的段。如果这样做,就不再需要唯一的单元标识符

在这里可以看到您添加的代码:

  • 根据
    indepath
    ,只能在
    cellForRowAt indepath:
    中获取两个单元格中的一个。因此,您可以将
    dequeueReusableCell
    放在
    if
    块中
  • 您仍然没有设置
    methodType
    的值(请参见上面的答案)
  • 术语
    类型
    令人困惑。根据相当常见的命名风格,我将其命名为
    methodSegmentControl
    (或
    methodSegment
  • 实际上,正如您所做的那样,创建两个单元格类是典型的:
    ListDataTableViewCell
    ListDataTableViewCell2
    。您现在可以摆脱cell.methodType.ishiden=true/false,因为只有cell 2需要段控件
  • 我希望这有助于解决问题。祝你好运


    您似乎缺乏如何在iOS中使用表和单元格的基本知识。我建议你通读一本好的教程。通过反复试验,我将为您节省大量时间。分段控件不会随机翻转。我可以告诉你,这发生在你用段控制滚动单元格到足够远的地方,然后再滚动回来之后

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

    假设您没有很长的表,我的猜测是您没有一个专用的单元标识符,仅用于具有段控件的单元。如果是这样的话,试着给分割的单元格一个唯一的标识符;这个问题应该消失

    但这还不够。如果您的分段单元看不见(足够长/足够远),它仍可能被解除分配。因此,您需要将段控件选择存储在视图控制器中的某个位置,并在
    cellForRowAt
    中设置选定的段。如果这样做,就不再需要唯一的单元标识符

    在这里可以看到您添加的代码:

  • 根据
    indepath
    ,只能在
    cellForRowAt indepath:
    中获取两个单元格中的一个。因此,您可以将
    dequeueReusableCell
    放在
    if
    块中
  • 您仍然没有设置
    methodType
    的值(请参见上面的答案)
  • 术语
    类型
    令人困惑。根据相当常见的命名风格,我将其命名为
    methodSegmentControl
    (或
    methodSegment
  • 实际上,正如您所做的那样,创建两个单元格类是典型的:
    ListDataTableViewCell
    ListDataTableViewCell2
    。您现在可以摆脱cell.methodType.ishiden=true/false,因为只有cell 2需要段控件
  • 我希望这有助于解决问题。祝你好运


    您似乎缺乏如何在iOS中使用表和单元格的基本知识。我建议你通读一本好的教程。通过反复尝试,我将为您节省大量时间。

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