【发布时间】: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