ios - TableView 仅显示一个单元格 - Swift 3

标签 ios uitableview swift3

现在,我有 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]

最佳答案

您对于单元格和行以实现您想要的目标感到非常困惑。声明两种自定义类型或内置类型的单元格。

  1. 在 numberofrowsForIndexPath 方法中检查条件并返回 细胞计数
  2. 在您的 cellforRowAtIndexPath 中,根据您的模式返回相应的单元格,具体取决于模式
  3. 在更改模式时调用 tableView.reloadData()

关于ios - TableView 仅显示一个单元格 - Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47908498/

相关文章:

ios - 如何在集合单元格中重绘动态大小的 UIView

ios - 如何在iOS中以进度方式在后台下载多个mp4文件?

ios - UITableView 仅反弹底部边缘

ios - 当我的应用程序启动时,如何避免中断已经在播放的音频?

ios - 如何在 iPhone 中单击按钮时显示 TableView(如下拉菜单)?

ios - 我可以使用 NSUserDefaults 保存应用程序的表格单元格值吗?

IOS [__NSPlaceholderArray initWithObjects :count:]: attempt to insert nil object from objects[0]'

java - 我正在实例化从 Java (J2ObjC) 转译的 Swift 非 ARC Objective-C 类,它是否泄漏安全?

ios - 如何将 3D 对象加载到 View Controller 中?

ios - Xcode 8.1 中的 "Segmentation fault: 11"