简体   繁体   English

“尝试将第0行插入第0部分,但更新后第0部分只有0行”

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

Trying to fetch the images store in firebase and display it in tableview but it showing error 尝试获取Firebase中存储的图像并在tableview中显示它,但显示错误

"uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'" “未捕获到异常'NSInternalInconsistencyException',原因:'试图将第0行插入第0节,但更新后第0节只有0行'”

class galleryVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var tableVC: UITableView!


    var images = [imageUpload]()

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.backBarButtonItem?.title = "Camera"
        Database.database().reference().child("CameraPhoto").observe(.childAdded){ (snapshot) in

            DispatchQueue.main.async {
                let newImage = imageUpload(snapshot: snapshot)
                self.images.insert(newImage, at: 0)
                let indexpath = IndexPath(row: 0, section: 0)
                self.tableVC.insertRows(at: [indexpath], with: .top)
            }
        }
    }

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

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableVC.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellVC
        cell.post = self.images[indexPath.row]
        cell.selectionStyle = .none

        return cell
    }
}

code for the ImageUpload method used to upload image:- 用于上传图像的ImageUpload方法的代码:-

class imageUpload
{
var imageDownloadUrl:String?
private var image :UIImage!
var label1:String!
var label2:String!
var label3:String!
var label4:String!
init(image:UIImage,label1:String,label2:String,label3:String,label4:String) {
    self.image = image
    self.label1 = label1
    self.label2 = label2
    self.label3 = label3
    self.label4 = label4
}

init(snapshot: DataSnapshot) {
    let json = JSON(snapshot.value)
    self.imageDownloadUrl = json["imgDownloadURL"].stringValue
    self.label1 = json["label1"].stringValue
    self.label2 = json["label2"].stringValue
    self.label3 = json["label3"].stringValue
    self.label4 = json["label4"].stringValue
}




func upload()
{
    //1.new Database Refrence
    let newImg = Database.database().reference().child("CameraPhoto").childByAutoId()
    let newImgKey = newImg.key



     //convert image into data
    if let imgData = self.image.jpegData(compressionQuality: 0.6){

        //2.new Storage Refrence
        let ImgStorageRef = Storage.storage().reference().child("images")
        let newImgRef = ImgStorageRef.child(newImgKey!)

        //save the image into storage

        newImgRef.putData(imgData).observe(.success) { (snapshot) in
                //save the label and downloadURL()
            snapshot.reference.downloadURL(completion: { (url, error) in
                self.imageDownloadUrl = url?.absoluteString
                let newImgDictionary = [
                    "imgDownloadURL" :self.imageDownloadUrl,
                    "label1":self.label1,
                    "label2":self.label2,
                    "label3":self.label3,
                    "label4":self.label4
                ]

                newImg.setValue(newImgDictionary)
            })
        }
    }   
}

} }

Use this: 用这个:

self.tableVC.beginUpdates()
self.tableVC.insertRows(at: [indexpath], with: .top)
self.tableVC.endUpdates()

暂无
暂无

声明:本站的技术帖子网页,遵循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 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” “尝试将第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 尝试将第 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" 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