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

enter image description here

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...