这是我的代码。我检查了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
}
}
我尝试在我的上层和下层TableView中显示标签。但什么都没有
1条答案
按热度按时间a1o7rhls1#
您应该删除
upperTableView.register
和lowerTableView.register
调用。您已经通过将单元格添加到故事板并为其提供标识符来注册它们。如果您在代码中调用
register
,标识符将被注册到单元格类,而不是您在故事板中设计的视图。当你将一个单元移出队列时,它不会在故事板中创建一个单元的示例(它会调用你的单元类中的其他init
),所以你声明的outlet都是nil
。