This is my code. I checked IBOulets connection, Identifier, class names. and find some question and answer which on same boat. but didn't words. what did I missed? Thank you for reading. :)
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
}
}
I tried show labels in my upper and lower TableView. but there is nothing showed up.
You should remove the upperTableView.register
and lowerTableView.register
calls. You already registered the cells by adding them to the storyboard and giving them identifiers.
If you call register
in code, the identifiers will instead be registered to the cell class, not to the views you designed in the storyboard. When you dequeue a cell, it would not create an instance of the cell in the storyboard (it would call one of the other init
s your cell class has), and so the outlets you declared are all nil
.