[英]'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.