繁体   English   中英

返回 nil 的自定义 tableview 单元格 xib 出口

[英]custom tableview cell xib outlets returning nil

我有一个带有表格视图的视图 controller。 我想用我用 xib 文件以编程方式创建的自定义 uitableviewcell 注册我的 tableview。 在自定义单元格中,我有一个 function 会更改插座标签值,这就是我遇到致命发现 nil 错误的地方。 这是我的视图 controller 的代码。

    class AllTasksViewController: UIViewController{
    
    var tableView = UITableView()
    
    func configureTableView(){
        view.addSubview(tableView)
        setTableViewDelegates()
        tableView.rowHeight = 50
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        tableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
        tableView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        tableView.register(CustomCellNSB2.self, forCellReuseIdentifier: "CustomCellNSB2")
    }
    func setTableViewDelegates(){
        tableView.delegate = self
        tableView.dataSource = self
    }
    
    

    

        override func viewDidLoad(){
            super.viewDidLoad()
            
            configureTableView()
            print("allTasksView loaded")
                        //add some positioning and size constraints here for allTasksView
        }
    }

extension AllTasksViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return allTasks.count
    }
    
    
    // Cell Data and Configuration
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let task = allTasks[indexPath.row] // creating a new task from the already stored task depending on the indexpath.row if indexPath.row is 3 then the task is tasks[3]
                let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellNSB2", for: indexPath) as! CustomCellNSB2 // setting the identifier ( we have already set in the storyboard, the class of our cells to be our custom cell)
                        cell.setTask(task: task) `// this is where my app is crashing`
                        if (task.value(forKey: "isComplete") as! Bool == true){
                        cell.labelsToWhite()
                        cell.backgroundColor = Colors.greencomplete
                        cell.selectionStyle = .none
                    
                    } else {
                        cell.backgroundColor = .white //adjust for nightmode later
                        cell.labelsToBlack()
                }
                

                
                
                print("CellData Task :", task.value(forKey: "isComplete") as! Bool, task.value(forKey: "name") as! String)
                

        //        if !(task.value(forKey: "isComplete") as! Bool){
        //            cell.backgroundColor = task.isImportant ? .purple : .white //according to the task isImportant attribute we set the background color
        //        }
                
                return cell
        
    }

这是我的定制 uitableview 单元

import UIKit
import CoreData
class CustomCellNSB2: UITableViewCell {
    @IBOutlet weak var taskLabel: UILabel!
    @IBOutlet weak var dateLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func setTask(task: NSManagedObject ){ debugger leads me here. this is where it's getting nil
        taskLabel.text = task.value(forKey: "name") as? String
        dateLabel.text = task.value(forKey: "date") as?
        String

    }

    func labelsToWhite() {
        taskLabel.textColor = .white
        dateLabel.textColor = .white
    }

    func labelsToBlack() {
        taskLabel.textColor = .black
        dateLabel.textColor = .red
    }
}
    

你必须注册你的NIB……你所做的就是注册class。

改变这个:

tableView.register(CustomCellNSB2.self, forCellReuseIdentifier: "CustomCellNSB2")

对此:

tableView.register(UINib(nibName: "CustomCellNSB2", bundle: nil), forCellReuseIdentifier: "CustomCellNSB2")

如果您正确配置了 xib/nib,那么您可能只需要这样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM