TableView只显示一个单元格--Swift 3

时间:2017-12-20 14:41:59

标签: ios uitableview swift3

现在,我有2种不同的单元格类型,当启用名为newMode的属性时,我的代码在2之间切换。代码如下。

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
    }

这是切换单元格时发生的情况。但问题是,现在我的单元格一次只显示一个项目。当我删除该项并刷新视图控制器时,它会显示另一项。为什么不显示我用于填充视图控制器的数组中的所有项目。这发生在之前,但在我开始进行单元格切换后不会发生。我究竟做错了什么?

let expense = monthlyExpenses[indexPath.row]

1 个答案:

答案 0 :(得分:1)

你对细胞和行的困惑很大,以达到你想要的效果。声明两个自定义类型或内置类型的单元格。

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