2016-12-11 2 views
-1

Xcodeの8.1、スウィフト2.3、iOSの10.1、そして、私はFirebaseスウィフト - 「セクション0に行0を挿入しようとする試みが、セクション0で唯一の0行が更新後にある」

を使用し、私はfirebaseを使用して通知を登録。そして私はuitableviewに関するショーの通知をしようとしています。 viewDidLoad()正常にfirebaseに接続し、値を取得します。しかし、私は入ってくるデータを列挙できません。

最初に「cellForRowAtIndexPathが機能しません」というエラーが表示されていました。その後、forRow & inSectionを使用します。しかし今、私はそれが何を意味するのか分からないというエラーを受けています。

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 

    } 

} 
+0

1)エラーの原因となった実際のコードで質問を更新し、エラーの原因となった正確な行を指摘してください。 2)エラーを検索します。それは何度も議論されてきました。 – rmaddy

答えて

0

NoticeViewController.swift

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

あなたのコード内でのミスがたくさんあります。それらのいずれかがクラッシュする可能性があります。

  • uidが無効であっても、テーブルビューに行が挿入されます。
  • detailsデータソースアレイにしかし添付
  • reloadData()insertRowsAtIndexPaths両方を呼び出さないテーブルビューのインデックス0に挿入あります。削除するreloadData()
関連する問題