我正在制作iOS应用,但我一直收到此错误:
'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 sections after the update'
我已经研究了这个问题2天了,但我不知道出了什么问题。我把我的代码放在下面。
import UIKit
import FirebaseDatabase
import FirebaseAuth
class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var databaseRef = Database.database().reference()
var loggedInUser:AnyObject?
var loggedInUserData:NSDictionary?
@IBOutlet weak var aivLoading: UIActivityIndicatorView!
@IBOutlet weak var homeTableView: UITableView!
var posts = [NSDictionary]()
override func viewDidLoad() {
super.viewDidLoad()
self.loggedInUser = Auth.auth().currentUser
self.databaseRef.child("users").child(self.loggedInUser!.uid).observeSingleEvent(of: .value) { (snapshot: DataSnapshot) in
self.loggedInUserData = snapshot.value as? NSDictionary
self.databaseRef.child("posts").child(self.loggedInUser!.uid).observe(.childAdded, with: { (snapshot: DataSnapshot) in
self.posts.insert(snapshot.value as! NSDictionary, at: 0)
self.homeTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: UITableViewRowAnimation.automatic)
self.aivLoading.stopAnimating()
}){(error) in
ProgressHUD.showError("There was an error, try again")
}
}
self.homeTableView.rowHeight = UITableViewAutomaticDimension
self.homeTableView.estimatedRowHeight = 140
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: HomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
let post = posts[(self.posts.count-1) - (indexPath.row)]["text"] as! String
cell.configure("Anonymous", post: post)
return cell
}
}
在我的代码中,它引用了一个dequeueReusableCell,其标识符为HomeTableViewCell
,这是一个Swift文件。这是Swift文件的代码:
import UIKit
import FirebaseDatabase
import Firebase
import FirebaseAuth
class HomeTableViewCell: UITableViewCell {
@IBOutlet weak var nameConstant: UILabel!
@IBOutlet weak var post: UITextView!
override open func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
open func configure(_ nameConstant: String, post: String) {
self.post.text = post
self.nameConstant.text = "Anonymous"
}
}
我知道我的问题也有类似问题,但很多问题都在Objective-C中。我也看过Swift,但他们没有帮助。
如果有人有任何想法来解决这个问题会很棒。提前谢谢!
答案 0 :(得分:1)
在您的代码中,您总是返回1行数。但我想你应该回来post.count吧?每次insertRows(at:...
表都通过委托检查一致性。还有一些奇怪的东西,你要附加到你的列表(self.posts.append(snapshot.value as! NSDictionary)
)但是你要插入0-0行?我会假设你要插入新帖子到顶部。此外,如果您只有一个部分,则无需实施该方法。
所以这是代码(我假设firebase和单元配置代码是正确的):
import UIKit
import FirebaseDatabase
import FirebaseAuth
class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var databaseRef = Database.database().reference()
var loggedInUser:AnyObject?
var loggedInUserData:NSDictionary?
@IBOutlet weak var aivLoading: UIActivityIndicatorView!
@IBOutlet weak var homeTableView: UITableView!
var posts = [NSDictionary]()
override func viewDidLoad() {
super.viewDidLoad()
self.loggedInUser = Auth.auth().currentUser
self.databaseRef.child("users").child(self.loggedInUser!.uid).observeSingleEvent(of: .value) { (snapshot: DataSnapshot) in
self.loggedInUserData = snapshot.value as? NSDictionary
self.databaseRef.child("posts").child(self.loggedInUser!.uid).observe(.childAdded, with: { (snapshot: DataSnapshot) in
self.posts.insert(snapshot.value as! NSDictionary, at: 0)
self.homeTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: UITableViewRowAnimation.automatic)
self.aivLoading.stopAnimating()
}){(error) in
ProgressHUD.showError("There was an error, try again")
}
}
self.homeTableView.rowHeight = UITableViewAutomaticDimension
self.homeTableView.estimatedRowHeight = 140
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: HomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
let post = posts[(self.posts.count-1) - (indexPath.row)]["text"] as! String
cell.configure("Anonymous", post: post)
return cell
}
}
我希望它能解决你的问题;)