我在 swift 3 中遇到错误
I've got an error in swift 3
我是 swift 3 的新手,现在我遇到一个错误,我找不到答案...
我想在我的自定义设计和我想注册我的 xib 文件的部分中创建一个 table 视图。
// ViewController.swift
// Flash Chat
import UIKit
import Firebase
class ChatViewController: UIViewController , UITableViewDelegate , UITableViewDataSource {
@IBOutlet var heightConstraint: NSLayoutConstraint!
@IBOutlet var sendButton: UIButton!
@IBOutlet var messageTextfield: UITextField!
@IBOutlet var messageTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
messageTableView.delegate = self
messageTableView.dataSource = self
messageTableView.register(UINib(nibName : "MessageCell" , Bundle : nil), forCellReuseIdentifier: "customMessageCell")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customMessageCell", for: indexPath) as! CustomMessageCell
let messageArray = ["First Message", "Second Message", "Third Message"]
cell.messageBody.text = messageArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
@IBAction func sendPressed(_ sender: AnyObject) {
}
@IBAction func logOutPressed(_ sender: AnyObject) {
do {
try FIRAuth.auth()?.signOut()
} catch {
print(error)
}
guard (navigationController?.popToRootViewController(animated: true)) != nil
else {
print("ther is no view controller")
return
}
}
}
第二个参数标签必须是'bundle',而不是'Bundle,'见https://developer.apple.com/documentation/uikit/uinib/1614135-init。
您在 UINib 初始化中输入错误,替换
UINib(nibName : "MessageCell" , Bundle : nil)
和
UINib(nibName : "MessageCell" , bundle : nil)
是打字错误,在 init(nibName:bundle:) 中调用的 Initializer 不是 init(nibName:Bundle:)
是
messageTableView.register(UINib(nibName : "MessageCell" , bundle : nil), forCellReuseIdentifier: "customMessageCell")
或使用
messageTableView.register(UINib.init(nibName: "MessageCell", bundle: nil), forCellReuseIdentifier: "customMessageCell")
不是
messageTableView.register(UINib(nibName : "MessageCell" , Bundle : nil), forCellReuseIdentifier: "customMessageCell")
更正这个
messageTableView?.register(UINib.init(nibName: "MessageCell", bundle: nil), forCellReuseIdentifier: "customMessageCell")
将您的手机注册声明更改为以下内容:
messageTableView.register(UINib.init(nibName: "MessageCell",
bundle: nil),
forCellReuseIdentifier: "customMessageCell") //Just copy-paste this
您为 UINib
构造函数使用了错误的方法参数标签。将 Bundle
更改为 bundle
即可。打字错误。
我是 swift 3 的新手,现在我遇到一个错误,我找不到答案... 我想在我的自定义设计和我想注册我的 xib 文件的部分中创建一个 table 视图。
// ViewController.swift
// Flash Chat
import UIKit
import Firebase
class ChatViewController: UIViewController , UITableViewDelegate , UITableViewDataSource {
@IBOutlet var heightConstraint: NSLayoutConstraint!
@IBOutlet var sendButton: UIButton!
@IBOutlet var messageTextfield: UITextField!
@IBOutlet var messageTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
messageTableView.delegate = self
messageTableView.dataSource = self
messageTableView.register(UINib(nibName : "MessageCell" , Bundle : nil), forCellReuseIdentifier: "customMessageCell")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customMessageCell", for: indexPath) as! CustomMessageCell
let messageArray = ["First Message", "Second Message", "Third Message"]
cell.messageBody.text = messageArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
@IBAction func sendPressed(_ sender: AnyObject) {
}
@IBAction func logOutPressed(_ sender: AnyObject) {
do {
try FIRAuth.auth()?.signOut()
} catch {
print(error)
}
guard (navigationController?.popToRootViewController(animated: true)) != nil
else {
print("ther is no view controller")
return
}
}
}
第二个参数标签必须是'bundle',而不是'Bundle,'见https://developer.apple.com/documentation/uikit/uinib/1614135-init。
您在 UINib 初始化中输入错误,替换
UINib(nibName : "MessageCell" , Bundle : nil)
和
UINib(nibName : "MessageCell" , bundle : nil)
是打字错误,在 init(nibName:bundle:) 中调用的 Initializer 不是 init(nibName:Bundle:)
是
messageTableView.register(UINib(nibName : "MessageCell" , bundle : nil), forCellReuseIdentifier: "customMessageCell")
或使用
messageTableView.register(UINib.init(nibName: "MessageCell", bundle: nil), forCellReuseIdentifier: "customMessageCell")
不是
messageTableView.register(UINib(nibName : "MessageCell" , Bundle : nil), forCellReuseIdentifier: "customMessageCell")
更正这个
messageTableView?.register(UINib.init(nibName: "MessageCell", bundle: nil), forCellReuseIdentifier: "customMessageCell")
将您的手机注册声明更改为以下内容:
messageTableView.register(UINib.init(nibName: "MessageCell",
bundle: nil),
forCellReuseIdentifier: "customMessageCell") //Just copy-paste this
您为 UINib
构造函数使用了错误的方法参数标签。将 Bundle
更改为 bundle
即可。打字错误。