这是我的密码.我判断了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
}
}
我try 在上方和下方的TableView中显示标签. 但什么也没有出现.