Swift 3打破索引路径的行 [英] Swift 3 Breaks Cell For Row At Index Path
问题描述
这段代码来自一个非常不活跃的教程,帮助我将数据加载到表视图。由于本教程是用Swift 2.0编写的,我相信这在Swift 3中已经改变了。我知道override函数本身已经被改变了,我处理了。但现在,它带给我一个线程1:EXC_BAD_INSTRUCTION(code = EXC_1386_INVOP,子码= 0x0)
错误。
更新:我尝试了多种方法,包括为单元格创建自定义类。我仍然得到上面列出的同样的错误,或者我的应用程序委托文件的第一行上的线程1:信号SIGABRT
错误。创建一个断点并没有帮助我,因为我知道错误来自哪里。
导入UIKit
导入Firebase
导入FirebaseDatabase
struct postStruct {
let title:String!
让消息:字符串!
class LoggedInController:UITableViewController {
var posts = [postStruct]()
覆盖func viewDidLoad(){
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
let databaseRef = FIRDatabase.database()。参考()
databaseRef.child(Posts)。queryOrderedByKey()。observe(.childAdded,with:{
快照
$ b $ let snapshotValue = snapshot .value as?NSDictionary
let title = snapshotValue![title] as?String
let message = snapshotValue![message] as?String
self.posts.insert(postStruct(title:title,message:message),at:0)
self.tableView.reloadData()
})
post()
$ b $ func post(){
let title =Title
let message =Message
let post:[String:AnyObject] = [title:title as AnyObject,
message:message as AnyObject]
let databaseRef = FIRDatabase.database()。reference()
$ b $ databaseRef.child(Posts)。childByAutoId()。setValue(post)
覆盖func tableView(_ tableView:UITableView ,numberOfRowsInSection部分:Int) - > Int {
return posts.count
}
覆盖func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:PostCell)
let label1 = cell?.viewWithTag(1)as! UILabel
label1.text = posts [indexPath.row] .message
let label2 = cell?.viewWithTag(2)as! UILabel
label2.text = posts [indexPath.row] .message
return cell!
$ c
更新2:是我使用的新代码。这不是很漂亮,只有标题。
$ b $ pre $ 覆盖func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) - > Int {
return posts.count
}
覆盖func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier:Cell)
if cell == nil {
cell = UITableViewCell(style:.default,reuseIdentifier: )
cell?.textLabel?.text = posts [indexPath.row] .title
cell?.detailTextLabel?.text = posts [indexPath.row] .message
return cell!
} else {
let label1 = cell?.viewWithTag(1)as? UILabel
label1?.text = posts [indexPath.row] .title
let label2 = cell?.viewWithTag(2)as? UILabel
label2?.text = posts [indexPath.row] .message
return cell!
这个
func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell
{
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier:cellReuseIdentifier)as UITableViewCell!
让label1 = cell.viewWithTag(1)as! UILabel
label1.text = posts [indexPath.row] .message
let label2 = cell.viewWithTag(2)as! UILabel
label2.text = posts [indexPath.row] .message
return cell
}
已编辑
确保你做了这些事情
UITableViewController
$ b 在
viewDidLoad()
self.tableView.register(UITableViewCell.self,forCellReuseIdentifier:catCell)
let catNib:UINib = UINib.init(nibName:TableViewCategoryCell,bundle:nil )
self.tableView.register(catNib,forCellReuseIdentifier:TableViewCategoryCell)
code> cellForRowAt indexPath
let cell:OPM_AlarmTableViewCategoryCell = tableView.dequeueReusableCell(withIdentifier: OPM_AlarmTableViewCategoryCell)as! OPM_AlarmTableViewCategoryCell!
cell.categoryLabel?.text =Some Text
return cell
UITableviewCell.XIB
希望你做了这些事情
$ b
UITableviewCell
确保点出现,以便 @IBOutlet
与
xib标签连接
This code was from a now inactive tutorial that helped me load in data to a table view. Since the tutorial was written in Swift 2.0, I believe that this was changed in Swift 3. I know that the override function itself was changed, which I handled. But now, it brings me a Thread 1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0)
error.
Update: I have tried multiple things including creating a custom class for the cell. I still either get the same error I listed above, or a Thread 1: Signal SIGABRT
error on the first line of my App Delegate file. Creating a breakpoint hasn't helped me because I know where the error is coming from.
import UIKit
import Firebase
import FirebaseDatabase
struct postStruct {
let title : String!
let message : String!
}
class LoggedInController: UITableViewController {
var posts = [postStruct]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("Posts").queryOrderedByKey().observe(.childAdded, with: {
snapshot in
let snapshotValue = snapshot.value as? NSDictionary
let title = snapshotValue!["title"] as? String
let message = snapshotValue!["message"] as? String
self.posts.insert(postStruct(title: title, message: message), at: 0)
self.tableView.reloadData()
})
post()
}
func post(){
let title = "Title"
let message = "Message"
let post : [String : AnyObject] = ["title" : title as AnyObject,
"message": message as AnyObject]
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("Posts").childByAutoId().setValue(post)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell")
let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].message
let label2 = cell?.viewWithTag(2) as! UILabel
label2.text = posts[indexPath.row].message
return cell!
}
}
Update 2: Here is the new code I used. It's not pretty and only gets the title.
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell?.textLabel?.text = posts[indexPath.row].title
cell?.detailTextLabel?.text = posts[indexPath.row].message
return cell!
} else {
let label1 = cell?.viewWithTag(1) as? UILabel
label1?.text = posts[indexPath.row].title
let label2 = cell?.viewWithTag(2) as? UILabel
label2?.text = posts[indexPath.row].message
return cell!
}
}
Try this
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
let label1 = cell.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].message
let label2 = cell.viewWithTag(2) as! UILabel
label2.text = posts[indexPath.row].message
return cell
}
Edited
Make Sure you did these things
UITableViewController
In viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier:"catCell" )
let catNib : UINib = UINib.init(nibName: "TableViewCategoryCell", bundle: nil)
self.tableView.register(catNib, forCellReuseIdentifier: "TableViewCategoryCell")
In cellForRowAt indexPath
let cell : OPM_AlarmTableViewCategoryCell = tableView.dequeueReusableCell(withIdentifier:"OPM_AlarmTableViewCategoryCell" ) as! OPM_AlarmTableViewCategoryCell!
cell.categoryLabel?.text = "Some Text"
return cell
UITableviewCell.XIB Hope u did these things
UITableviewCell
Make sure the dot appears so that the @IBOutlet
is connected with the
xib label
这篇关于Swift 3打破索引路径的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!