这是我的密码.我判断了IBOulets连接、标识符、类名. 找到一些同舟共济的问题和答案.但没有说出话来. 我错过了什么吗?感谢您的阅读.:)

HomeViewController.swift

import UIKit

struct Label {
    let top: String
    let day: String
    let summary: String
}

class HomeViewController: UIViewController {

    @IBOutlet weak var upperTableView: UITableView!
    @IBOutlet weak var lowerTableView: UITableView!
    
    let upperCellID = "UpperTableViewCell"
    let lowerCellID = "LowerTableViewCell"
    
    var dateString: String!
    
    var labels: [Label] = [
        Label(top: "title1", day: "2023.10.1.", summary: "Test"),
        Label(top: "title2", day: "2023.10.2.", summary: "Test"),
        Label(top: "title3", day: "2023.10.3.", summary: "Test")
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        // Upper Table (Your To-Do)
        upperTableView.delegate = self
        upperTableView.dataSource = self
        upperTableView.register(UpperTableViewCell.self, forCellReuseIdentifier: upperCellID)
        
        // Lower Table (Your To-Do)
        lowerTableView.delegate = self
        lowerTableView.dataSource = self
        lowerTableView.register(LowerTableViewCell.self, forCellReuseIdentifier: lowerCellID)
    }
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destination.
     // Pass the selected object to the new view controller.
     }
     */
    
}


extension HomeViewController: UITableViewDelegate, UITableViewDataSource {
    
    // These are working!
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch tableView {
        case upperTableView:
            print("you tabbed me!(Upper)")
        case lowerTableView:
            print("Below!")
        default:
            print("outside of table.")
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return labels.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var returnCell: UITableViewCell?
        
        if tableView == self.upperTableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: self.upperCellID, for: indexPath) as! UpperTableViewCell
            cell.topLabel?.text = labels[indexPath.row].top
            returnCell = cell
        } else if tableView == lowerTableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: self.lowerCellID, for: indexPath) as! LowerTableViewCell
            cell.titleLabel?.text = labels[indexPath.row].top
            returnCell = cell
        }
        return returnCell!
    }
}

UpperTableViewCell.swift

import UIKit

class UpperTableViewCell: UITableViewCell {

    @IBOutlet weak var topLabel: UILabel!
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var summaryLabel: 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
    }

}

LowerTableViewCell.swift

import UIKit

class LowerTableViewCell: UITableViewCell {

    @IBOutlet weak var titleLabel: 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
    }

}

Identifier Example

The Screen

我try 在上方和下方的TableView中显示标签. 但什么也没有出现.

推荐答案

您应该删除upperTableView.registerlowerTableView.register呼叫.您already通过将单元格添加到情节提要并为其提供标识符来注册它们.

如果在代码中调用register,则标识符将注册到单元格类,而不是注册到您在情节提要中设计的视图.当您将一个单元格出队时,它将在情节提要中创建该单元格的一个实例(它将调用您的单元格类拥有的其他init之一),因此您声明的所有出口都是nil.

Ios相关问答推荐

如何在SWIFT中处理不可发送的类型?

Nomb iOS 17.4.0中崩溃

SWIFT AVFoundation翻转相机功能不起作用

在SwiftUI中插入绘制重复形状

为什么iOS委派在KMM中不能正常工作?

带菜单的选取器不适用于Int32,但适用于Int

iOS应用程序冻结在UICollectionView代码,但我没有';没有任何UICollectionViews

SwiftData共享扩展阻止应用程序打开SQLite文件

使用 SwiftUI 显示多个 VNRecognizedObjectObservation 边界框时偏移量错误

在 Swift 中 @MainActor 似乎被 withCheckedContinuation 而不是其他异步调用继承是巧合吗?

滚动 swiftUI 列表时,未调用单元格的任务修饰符.怎么修?

如何在 SwiftUI 中将选项卡放在滚动视图中?

如何在 SwiftUI 中检测 TextField 的实时变化?

如何从一个特定的视图控制器中隐藏导航栏

UITableView 背景 colored颜色 在 iPad 上总是白色

如何更改uitableview删除按钮文本

iOS7 UITextView contentsize.height 替代

在 NSUserDefaults 中存储值是否有任何限制?

请在您的 Podfile 中为此目标指定一个平台?

更改标签栏项目图像和文本 colored颜色 iOS