我的代码:
//
// ForumViewController.swift
// Kode4Kids
//
// Created by Caleb Clegg on 17/06/2020.
// Copyright © 2020 Group9. All rights reserved.
//
import UIKit
class ForumViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
class PostTableViewCell: UITableView{
@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var subtitleLabel: UILabel!
@IBOutlet weak var postTextLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
//initialization stuff
}
func set(post:Post) {
usernameLabel.text = post.author
postTextLabel.text = post.text
}
}
var tableView: UITableView!
var posts = [
Post(id: "1", author: "Ryan Johnston", text: "Kode4Kids Just Launched!"),
Post(id: "2", author: "Caleb Clegg", text: "Woah This Is Lit!!"),
Post(id: "3", author: "Habib Yusuf", text: "I LOVE THIS APP!"),
Post(id: "4", author: "Maggie Mogul", text: "Gonna learn some HTML"),
Post(id: "5", author: "Anthony Smith", text: "Just finished learning Python!"),
Post(id: "6", author: "Renee Miseer", text: "So excited to start learning!"),
Post(id: "7", author: "Alicia Keys", text: "Gonna stop singing to pick up code!"),
Post(id: "8", author: "Dwayne Johnson", text: "Ah great craic"),
Post(id: "9", author: "My dad", text: "okay"),
Post(id: "10", author: "Kaiser Chief", text: "Sweeet app!"),
Post(id: "12", author: "Darryl Granberry", text: "RUN IT UPP"),
Post(id: "13", author: "Future", text: "Big racks on this app!")
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView = UITableView(frame: view.bounds, style: .plain)
tableView.backgroundColor = UIColor.white
let cellNib = UINib(nibName: "ForumViewCell", bundle: nil)
tableView.register(cellNib, forCellReuseIdentifier: "ForumCell")
view.addSubview(tableView)
var layoutGuide:UILayoutGuide!
if #available(iOS 11.0, *){
layoutGuide = view.safeAreaLayoutGuide
} else {
//fallback on previous versions
layoutGuide = view.layoutMarginsGuide
}
tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()
}
@IBAction func backTapped(_ sender: Any) {
let homeViewController = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
self.view.window?.rootViewController = homeViewController
self.view.window?.makeKeyAndVisible()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ForumCell", for: indexPath) as! ForumViewController
return cell
cell.set(post: posts[indexPath.row])
}
}
正在努力解决cellForRow部分中出现的此错误。不确定我需要更改什么。我将不胜感激。这张图片显示了错误,直到我在“” ForumCell“中输入for” indexPath)as!ForumViewController“为止,都没有显示错误。我不明白为什么会显示错误
答案 0 :(得分:0)
您需要提供单元格而不是控制器
// it should be UITableViewCell instead of UITableView
class PostTableViewCell: UITableViewCell {
@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var subtitleLabel: UILabel!
@IBOutlet weak var postTextLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
//initialization stuff
}
func set(post:Post) {
usernameLabel.text = post.author
postTextLabel.text = post.text
}
}
class ForumViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView!
var posts = [
Post(id: "1", author: "Ryan Johnston", text: "Kode4Kids Just Launched!"),
Post(id: "2", author: "Caleb Clegg", text: "Woah This Is Lit!!"),
Post(id: "3", author: "Habib Yusuf", text: "I LOVE THIS APP!"),
Post(id: "4", author: "Maggie Mogul", text: "Gonna learn some HTML"),
Post(id: "5", author: "Anthony Smith", text: "Just finished learning Python!"),
Post(id: "6", author: "Renee Miseer", text: "So excited to start learning!"),
Post(id: "7", author: "Alicia Keys", text: "Gonna stop singing to pick up code!"),
Post(id: "8", author: "Dwayne Johnson", text: "Ah great craic"),
Post(id: "9", author: "My dad", text: "okay"),
Post(id: "10", author: "Kaiser Chief", text: "Sweeet app!"),
Post(id: "12", author: "Darryl Granberry", text: "RUN IT UPP"),
Post(id: "13", author: "Future", text: "Big racks on this app!")
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView = UITableView(frame: view.bounds, style: .plain)
tableView.backgroundColor = UIColor.white
let cellNib = UINib(nibName: "ForumViewCell", bundle: nil)
tableView.register(cellNib, forCellReuseIdentifier: "ForumCell")
view.addSubview(tableView)
var layoutGuide:UILayoutGuide!
if #available(iOS 11.0, *){
layoutGuide = view.safeAreaLayoutGuide
} else {
//fallback on previous versions
layoutGuide = view.layoutMarginsGuide
}
tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()
}
@IBAction func backTapped(_ sender: Any) {
let homeViewController = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
self.view.window?.rootViewController = homeViewController
self.view.window?.makeKeyAndVisible()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ForumCell", for: indexPath) as! PostTableViewCell
return cell
cell.set(post: posts[indexPath.row])
}
}
答案 1 :(得分:0)
此行:
let cell = tableView.dequeueReusableCell(withIdentifier: "ForumCell", for: indexPath) as! ForumViewController
用于使UITableViewCell或UITableViewCell的子类出队。您试图将其设置为无法正常工作的ForumViewController
。
如果创建了自定义单元格类,请改用该类。至少应该是这样的:
let cell = tableView.dequeueReusableCell(withIdentifier: "ForumCell", for: indexPath) as! PostTableViewCell