如何解决TableView未加载
//
// 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函数。
请告知!
解决方法
您在哪里设置分区号?似乎丢失了。
,在注册代表之后,在结束后尝试在viewDidLoad()方法中调用方法practicesTable.reloadData()
。有时,这可以奏效并回顾那些方法。
如果您使用的是故事板,请尝试在此处链接dataSource
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。