【发布时间】:2020-03-16 18:39:40
【问题描述】:
我是 Xcode 和 Swift 的新手,按照教程进行操作,当我在 tableview 前面调用 UIView 以便用户可以创建新内容时发现了一个问题
注意:我已经尝试过 link 显示的内容,但没有解决我的问题
我正在使用 Xcode 11.3.1 和 Swift
这是我的代码
渠道模型
import Foundation
struct Channel : Decodable {
public private(set) var channelTitle: String!
public private(set) var channelDescription: String!
public private(set) var id: String!
}
Class ChannelCell
import UIKit
class ChannelCell: UITableViewCell {
// Outlets
@IBOutlet weak var channelName: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
self.layer.backgroundColor = UIColor(white: 1, alpha: 0.2).cgColor
} else {
self.layer.backgroundColor = UIColor.clear.cgColor
}
}
func configureCell(channel: Channel) {
let title = channel.channelTitle ?? ""
channelName.text = "#\(title)"
}
}
频道视图控制器
import UIKit
class ChannelVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
// Outlets
@IBOutlet weak var loginBtn: UIButton!
@IBOutlet weak var userImg: CircleImage!
@IBOutlet weak var tableView: UITableView!
@IBAction func prepareForUnwind(segue: UIStoryboardSegue) {}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self <<<<<<<<< here I get the error message ***
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
tableView.dataSource = self
self.revealViewController()?.rearViewRevealWidth = self.view.frame.size.width - 60
NotificationCenter.default.addObserver(self, selector: #selector(ChannelVC.userDataDidChange(_:)), name: NOTIF_USER_DATA_DID_CHANGE, object: nil)
}
override func viewDidAppear(_ animated: Bool) {
setupUserInfo()
}
When I pressed the add channel button comes the problem
@IBAction func addChannelPressed(_ sender: Any) {
if AuthService.instance.isLoggedIn {
let addChannel = ChannelVC()
addChannel.modalPresentationStyle = .custom
present(addChannel, animated: true, completion: nil)
} else {
performSegue(withIdentifier: TO_LOGIN, sender: nil)
}
}
@IBAction func loginBtnPressed(_ sender: Any) {
if AuthService.instance.isLoggedIn {
let profile = ProfileVC()
profile.modalPresentationStyle = .custom
present(profile, animated: true, completion: nil)
} else {
performSegue(withIdentifier: TO_LOGIN, sender: nil)
}
}
@objc func userDataDidChange(_ notif: Notification) {
setupUserInfo()
}
func setupUserInfo() {
if AuthService.instance.isLoggedIn {
loginBtn.setTitle(UserDataService.instance.name, for: .normal)
userImg.image = UIImage(named: UserDataService.instance.avatarName)
userImg.backgroundColor = UserDataService.instance.returnUIColor(components: UserDataService.instance.avatarColor)
} else {
loginBtn.setTitle("Login", for: .normal)
userImg.image = UIImage(named: "menuProfileIcon")
userImg.backgroundColor = UIColor.clear
}
}
// Protocols for UITableViewDataSource
// # of sections
// # rows in the section
// function to setup the cells
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "channelCell", for: indexPath) as? ChannelCell {
I double check the reusable identifier is OK
let channel = MessageService.instance.channels[indexPath.row]
cell.configureCell(channel: channel)
return cell
} else {
return UITableViewCell()
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if MessageService.instance.channels.count == 0 {
tableView.setEmptyView(title: "Message!", message: "You don´t have any channel, create a new one")
}
return MessageService.instance.channels.count
}
}
这是我点击 addChannel 函数时想要显示的视图 view to present
这是调试区 debug area
【问题讨论】:
-
Don't repost questions。您是否阅读了副本中的信息?如果此时代码崩溃,则说明插座未连接。
-
它所连接的插座,在我发布我的第一个问题之前,我已经尝试过你给我的链接中的信息,在我广泛阅读其他帖子之前,我尽量不发布任何问题。抱歉,我不想惹恼任何人。
-
你在哪里注册
tableView cell? -
@Vadian 我按照你告诉我的做了,没有更多错误,但它并没有解决我的问题。我想我没有正确解释这个问题,我说我想在单击按钮时显示一个新的 UIView。这个 UIView 是一个 XIB 文件,我不知道这个新信息是否有帮助。
-
@HabinLama 你的意思是我在哪里声明牢房?我在 Main.storyboard 的帮助下完成了这项工作,我将 tableview 和 tableview 单元格放在了这个里面。