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

evrscar2  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(203)

Xcode 8.1,Swift 2.3,iOS 10.1,我用的是Firebase
我用firebase注册了通知。我尝试在uitableview上显示通知。viewDidLoad()成功连接firebase并获取值。但我无法列出传入的数据。
首先,我得到了错误“cellForRowAtIndexPath不工作”。之后,我使用forRow & inSection。但现在我得到了错误,我不知道它的意思。


***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

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

    }

}
daolsyd0

daolsyd01#

你的代码有很多错误。任何一个都可能导致崩溃。

  • 即使uid无效,也会在表格视图中插入一行。
  • details追加到数据源数组,但被插入表视图中的索引0处
  • 不要同时调用reloadData()insertRowsAtIndexPaths。删除reloadData()
62lalag4

62lalag42#

对于遇到此问题的其他用户,在尝试更新未分配dataSource的TableView或CollectionView时,也会出现这些错误。请确保TableView的dataSource已连接(使用情节提要或nib时)或已通过编程方式分配。

相关问题