简体   繁体   English

Swift-“尝试将第0行插入第0节,但更新后第0节只有0行”

[英]Swift - “attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update”

Xcode 8.1, Swift 2.3, iOS 10.1, And I use Firebase Xcode 8.1,Swift 2.3,iOS 10.1,我使用Firebase

I registered notices using firebase. 我使用Firebase注册了通知。 And I am trying show notices on uitableview. 我正在尝试在uitableview上显示通知。 viewDidLoad() succesfully connection firebase and get value. viewDidLoad()成功连接Firebase并获取值。 But I can not list the incoming data. 但是我无法列出传入的数据。

First I was getting the error "cellForRowAtIndexPath doesn't work". 首先,我收到错误消息“ cellForRowAtIndexPath不起作用”。 After, i use forRow & inSection. 之后,我使用forRow和inSection。 But now I'm getting the error that I do not know what it means. 但是现在我得到了一个错误,我不知道这意味着什么。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'

NoticeViewController.swift NoticeViewController.swift

import UIKit
import FirebaseDatabase
import FirebaseAuth
import FirebaseStorage

private let reuseIdentifier = "NoticeViewTable"

class NoticeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var aivLoading: UIActivityIndicatorView!
    @IBOutlet weak var noticeTableView: UITableView!

    var databaseRef = FIRDatabase.database().reference()
    var usersDict = NSDictionary()

    var noticesArray = [AnyObject]()
    var loggedInUser : AnyObject?



    @IBAction func didTapAddNotice(sender: AnyObject) {

        let mainStorboard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

        let viewController: UIViewController = mainStorboard.instantiateViewControllerWithIdentifier("AddNoticeView")
        self.presentViewController(viewController, animated: true, completion: nil)

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.loggedInUser = FIRAuth.auth()?.currentUser

        self.aivLoading.startAnimating()
        self.databaseRef.child("notice").observeEventType(.Value, withBlock: { (snapshot) in

            self.usersDict = snapshot.value as! NSDictionary

            self.noticesArray = [AnyObject]()

            for (userId, details) in self.usersDict {

                let noticeImg = details.objectForKey("noticeImage1") as! String
                let profileImg = details.objectForKey("profileImage") as! String
                let profileName =  details.objectForKey("userName") as! String
                let wage = details.objectForKey("wage") as! String
                let noticeName = details.objectForKey("noticeName") as! String

                if(self.loggedInUser?.uid != userId as? String){
                    details.setValue(userId, forKey: "uId")
                    self.noticesArray.append(details)
                }

                self.noticeTableView?.reloadData()

                self.noticeTableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Automatic)

                self.aivLoading.stopAnimating()


            }

        }) {(error) in
            print(error.localizedDescription)
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.noticesArray.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {

        let cell: NoticeViewTableViewCell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NoticeViewTableViewCell

        let profileImageURL = NSURL(string: self.noticesArray[indexPath.row]["profileImage"] as! String)
        let profileImageData = NSData(contentsOfURL: profileImageURL!)
        cell.profilePic.image = UIImage(data:profileImageData!)

        let noticeImageURL = NSURL(string: self.noticesArray[indexPath.row]["noticeImage!"] as! String)
        let noticeImageData = NSData(contentsOfURL: noticeImageURL!)
        cell.noticeImage.image = UIImage(data:noticeImageData!)


        //add a border and corner radius the images
        cell.profilePic.layer.masksToBounds = true
        cell.profilePic.layer.cornerRadius = cell.profilePic.frame.size.width/2.0
        cell.profilePic.layer.borderWidth = 1.5


        let profileName = (self.noticesArray[indexPath.row]["userName"] as? String)!
        cell.userName.text = profileName

        let noticeName = (self.noticesArray[indexPath.row]["noticeName"] as? String)!
        cell.noticeName.text = noticeName

        let wage = (self.noticesArray[indexPath.row]["wage"] as? String)!
        cell.wage.text = wage

        return cell

    }

}

There are a lot of mistakes in your code. 您的代码中有很多错误。 Any of them could cause the crash. 它们中的任何一个都可能导致崩溃。

  • A row is inserted in the table view even if uid is not valid. 即使uid无效,也会在表视图中插入一行。
  • details is appended to the datasource array but inserted at index 0 in the table view details附加到数据源数组,但在表视图中的索引0处插入
  • Do not call both reloadData() and insertRowsAtIndexPaths . 不要同时调用reloadData()insertRowsAtIndexPaths Delete reloadData() 删除reloadData()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “尝试将第0行插入第0部分,但更新后第0部分只有0行” - 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' 尝试将第0行插入第0部分,但更新后第0部分只有0行 - attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update 尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行 - attempt to insert row 3 into section 0, but there are only 3 rows in section 0 after the update “尝试将第0行插入第0部分,但更新后第0部分只有0行”错误 - “Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update” Error 尝试插入行时“尝试将第 0 行插入第 0 节,但更新后第 0 节中只有 0 行” - "attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update" when trying to insert row 在表中插入一行时,“试图将第0行插入第0部分,但更新后第0部分只有0行” - 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' when inserting a row into a table 'NSInternalInconsistencyException',原因:“试图将第0行插入第0节,但更新后第0节只有0行” - 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' 拖放时出现错误“尝试将第 4 行插入第 1 节,但更新后第 1 节中只有 4 行” - Getting an error "attempt to insert row 4 into section 1, but there are only 4 rows in section 1 after the update" while drop and drag 'NSInternalInconsistencyException',原因:“试图将第4行插入第0节,但更新后第0节只有4行” - 'NSInternalInconsistencyException', reason: 'attempt to insert row 4 into section 0, but there are only 4 rows in section 0 after the update' SwiftUI 应用程序因“尝试将第 2 行插入第 0 部分,但更新后第 0 部分中只有 2 行”错误而崩溃 - SwiftUI app crash with “attempt to insert row 2 into section 0, but there are only 2 rows in section 0 after the update” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM