现在,我有 2 种不同的单元格类型,并且当启用名为 newMode 的属性时,我的代码会在这两种单元格类型之间切换。代码如下。
else if indexPath.row>1{
let previousExpensesData = monthlyExpenses[indexPath.row - 1].modificationDate
let day = Calendar.current.component(.day, from: expense.modificationDate as! Date) // Do not add above 'date' value here, you might get some garbage value. I know the code is redundant. You can adjust that.
let previousDay = Calendar.current.component(.day, from: monthlyExpenses[indexPath.row - 1].modificationDate! as Date)
if day == previousDay {
newMode2 = false
} else {
newMode2 = true
}
}
newMode=newMode2
这就是声明模式切换的条件。
if newMode==true{
cell2.isHidden = true
cell.isHidden = false
let screenSize: CGRect = UIScreen.main.bounds
cell.dateLabelBackground.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: 31)
cell.dateLabel.text = dateString
cell.expenseName2.text = expense.name
cell.expenseAmount2.text = finalDisplayed
cell.expenseCategory2.text = expense.category
cell.expenseCollection2.text = expense.collection
if (expense.expense) {
cell.expenseAmount2.textColor = UIColor.red
}
else if (expense.income){
cell.expenseAmount2.textColor = UIColor.green
}
if (expense.cash) && (expense.expense){
cell.cashOrCredit.image = #imageLiteral(resourceName: "Cash-Expense Icon")
}
else if (expense.cash) && (expense.income){
cell.cashOrCredit.image = #imageLiteral(resourceName: "Cash-Income Icon")
}
else if (expense.credit) && (expense.income){
cell.cashOrCredit.image = #imageLiteral(resourceName: "Credit-Income Icon")
}
else if (expense.credit) && (expense.income){
cell.cashOrCredit.image = #imageLiteral(resourceName: "Credit-Expense Icon")
}
return cell
}
else if newMode==false{
cell.isHidden = true
cell2.isHidden = false
cell2.expenseName3.text = expense.name
cell2.expenseAmount3.text = finalDisplayed
cell2.expenseCategory3.text = expense.category
cell2.expenseCollection3.text = expense.collection
if (expense.expense) {
cell2.expenseAmount3.textColor = UIColor.red
}
else if (expense.income){
cell2.expenseAmount3.textColor = UIColor(red:0.49, green:0.83, blue:0.13, alpha:1.0)
}
if (expense.cash) && (expense.expense){
cell2.cashOrCredit3.image = #imageLiteral(resourceName: "Cash-Expense Icon")
}
else if (expense.cash) && (expense.income){
cell2.cashOrCredit3.image = #imageLiteral(resourceName: "Cash-Income Icon")
}
else if (expense.credit) && (expense.income){
cell2.cashOrCredit3.image = #imageLiteral(resourceName: "Credit-Income Icon")
}
else if (expense.credit) && (expense.income){
cell2.cashOrCredit3.image = #imageLiteral(resourceName: "Credit-Expense Icon")
}
return cell2
}
else {
return cell
}
这就是单元切换时发生的情况。但问题是,现在我的单元格一次只显示一项。当我删除该项目并刷新 View Controller 时,它会显示另一个项目。为什么没有显示我用来填充 View Controller 的数组中的所有项目。这种情况以前发生过,但在我开始小区切换后就不再发生了。我究竟做错了什么?
let expense = monthlyExpenses[indexPath.row]
最佳答案
您对于单元格和行以实现您想要的目标感到非常困惑。声明两种自定义类型或内置类型的单元格。
- 在 numberofrowsForIndexPath 方法中检查条件并返回 细胞计数
- 在您的 cellforRowAtIndexPath 中,根据您的模式返回相应的单元格,具体取决于模式
- 在更改模式时调用 tableView.reloadData()
关于ios - TableView 仅显示一个单元格 - Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47908498/