问题描述
//
// PracticesViewController.swift
// COVID 19 Safety
//
// Created by Rishi Kayathi on 8/15/20.
// Copyright © 2020 Rishi Kayathi. All rights reserved.
//
import UIKit
class PracticesViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
var practiceType = "good"
let goodPractices = practices.good().directions
let badPractices = practices.bad().directions
@IBOutlet weak var practicesTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
practicesTable.delegate = self
practicesTable.dataSource = self
practicesTable.register(UINib(nibName: "PracticesTableViewCell",bundle: nil),forCellReuseIdentifier: "practiceCell")
}
@IBAction func typePractice(_ sender: UIButton) {
if let temp = sender.accessibilityLabel{
practiceType = temp
print(practiceType)
}
practicesTable.reloadData()
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
if practiceType == "good" {
return goodPractices.count
} else {
return badPractices.count
}
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "practiceCell",for: indexPath) as! PracticesTableViewCell
if practiceType == "good" {
cell.checkmarkOrCross.image = UIImage(named: "checkmark")
cell.directions.text = goodPractices[indexPath.row]
cell.directions.textColor = UIColor.systemGreen
return cell
}
else {
cell.checkmarkOrCross.image = UIImage(named: "xmark")
cell.directions.text = badPractices[indexPath.row]
cell.directions.textColor = UIColor.systemRed
return cell
}
}
}
我使用打印功能检查了代码的每个部分,以确保其正常工作。但是,未调用cellForRowAt函数。 goodPractices是具有3个条目的字符串数组,badPractices是具有2个条目的字符串数组。它们分别以节的行数返回3和2,但是由于某些原因,未调用cellForRowAt函数。
请告知!