I'm new to swift, I made a simple table and added 21 data in it.
When I debug, the compiler enters here, I see that there are 21.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(menus.count)
return menus.count
}
but it doesn't go into this part
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "mCell", for: indexPath) as! HomeViewCell
let men = menus[indexPath.row]
print(men.Title)
cell.mTitle.text = men.Title
cell.subTitle.text = men.SubTitle
return cell
}
I'm learning new things from books, videos, sorry for the newbie question
I think I've done all the work but I couldn't figure out what I'm skipping I watched too many videos and I'm confused
all code,
class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var menuTable: UITableView!
var menus = [Menu]()
override func viewDidLoad() {
super.viewDidLoad()
loadMenus()
}
override func viewDidLayoutSubviews(){
}
func loadMenus(){
for index in 0...20 {
menus.insert(Menu(Image: "0", Title: "String \(index)", SubTitle: "String"), at: index)
}
self.menuTable.reloadData()
// DispatchQueue.main.async {
//
// }
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(menus.count)
return menus.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "mCell", for: indexPath) as! HomeViewCell
let men = menus[indexPath.row]
print(men.Title)
cell.mTitle.text = men.Title
cell.subTitle.text = men.SubTitle
return cell
}
}
class HomeViewCell: UITableViewCell {
@IBOutlet weak var mTitle: UILabel!
@IBOutlet weak var subTitle: UILabel!
@IBOutlet weak var mImage: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
CodePudding user response:
I tried your code and everything works well.
The only thing I can think of:
Make sure you have assigned the class to the table view cell and connected the outlets.
What you showed me above is the identifier. See below for class.
After I do this, I see the result:
Output in the console
21
21
21
String 0
String 1
String 2
String 3
String 4
String 5
String 6
String 7
String 8
String 9
String 10
String 11
String 12
String 13
String 14
Can you confirm you assigned HomeViewCell
class to the Cell in storyboard ?
Update
You use auto layouts to set up your table and give the cell a height of 138 but you do not add any constraints for the subviews within your cell so I believe the runtime is unable to determine the actual height of your cell if the subviews in the cell do not have proper constraints.
add this UITableView delegate method and run your app to see if you see results:
func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath) -> CGFloat {
return 138
}
Of Course, change 138 to whatever you want or better yet configure your cell layout in autolayout.
End result:
CodePudding user response:
Try adding this in viewDidLoad:
menuTable.register(HomeViewCell.self, forCellReuseIdentifier: "mCell")