我有一个填充数组的tableview。我可以添加到firebase和tableview重新加载以显示新添加的对象。如果我在firebase中有5个项目,那么我会在tableview上有5个项目。我可以通过代码从firebase中删除项目并重新加载tableview,它运行良好。
我的问题是,当我在firebase和tableview上的最后一项时,我删除了最后一项,firebase将其删除就好了,但是tableview会保留最后一项,但会将其灰显。
应用程序不会崩溃它只是停留在那里直到我添加一些东西。
显然,如果我点击那个灰色的单元格我的应用程序崩溃了,因为我正在点击一个超出范围的索引。
是否需要添加一些代码来防止灰显的单元格并且只有一个空的tableview?
$PropertiesObject = @{
"enabled" = "True";
"unauthenticatedClientAction" = "0";
"defaultProvider" = "0";
"tokenStoreEnabled" = "True";
"clientId" = "<your client ID here>";
"issuer" = "https://sts.windows.net/<your AAD ID here>/";
"allowedAudiences" = "{https://<service name>.azurewebsites.net}";
"isAadAutoProvisioned" = "True";
"aadClientId" = "<your client ID here>";
"openIdIssuer" = "https://sts.windows.net/<your AAD ID here>/";
}
New-AzureRmResource -PropertyObject $PropertiesObject `
-ResourceGroupName $rgName -ResourceType $resourceType `
-ResourceName $resourcename -ApiVersion 2015-08-01 -Force
我必须错过一个小条件,因为除此之外,tableview与firebase完美配合......
修改1
我添加了用于填充tableview的代码
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return serviceArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "serviceCell", for: indexPath as IndexPath) as! ServiceTableViewCell
let row = indexPath.row
// cell.serviceLogoImage.image = UIImage.init(named: serviceArray[row].serviceUrl!)
cell.serviceNameLabel.text = serviceArray[row].serviceName
if serviceArray[row].serviceStatus == true {
cell.serviceStatusView.backgroundColor = .green
} else {
cell.serviceStatusView.backgroundColor = .red
}
return cell
}
}
修改2
我有想法检查firebase节点是否会退出(因为我刚删除它所以不应该这样做。所以
var serviceArray: [ServiceClass] = []
func pullCardData() {
serviceArray.removeAll()
let cardRef = ref.child("cards")
cardRef.observeSingleEvent(of: .value, with: { snapshot in
for cards in snapshot.children {
let allCardIDs = (cards as AnyObject).key as String
if allCardIDs == self.cardID {
let thisCardLocation = cardRef.child(self.cardID)
thisCardLocation.observeSingleEvent(of: .value, with: { snapshot in
let thisCardDetails = snapshot as FIRDataSnapshot
let cardDict = thisCardDetails.value as! [String: AnyObject]
self.selectedCard.cardID = thisCardDetails.key
self.selectedCard.nickname = cardDict["nickname"] as! String
self.selectedCard.type = cardDict["type"] as! String
self.cardNickNameLabel.text = cardDict["nickname"] as? String ?? ""
let thisCardServices = self.ref.child("cards").child(self.cardID).child("services")
let serviceRefLoc = self.ref.child("services")
thisCardServices.observeSingleEvent(of: .value, with: {serviceSnap in
if serviceSnap.hasChildren() {
for serviceChild in serviceSnap.children {
let serviceID = (serviceChild as AnyObject).key as String
serviceRefLoc.observeSingleEvent(of: .value, with: {allServiceSnap in
if allServiceSnap.hasChildren() {
for all in allServiceSnap.children {
let allServs = (all as AnyObject).key as String
let thisServiceLocationInServiceNode = self.ref.child("services").child(serviceID)
if serviceID == allServs {
thisServiceLocationInServiceNode.observeSingleEvent(of: .value, with: {thisSnap in
let serv = thisSnap as FIRDataSnapshot
let serviceDict = serv.value as! [String: AnyObject]
let aService = ServiceClass()
self.serviceCurrent = serviceDict["serviceStatus"] as? Bool
self.serviceName = serviceDict["serviceName"] as? String
self.serviceURL = serviceDict["serviceURL"] as? String
self.serviceFixedBool = serviceDict["serviceFixed"] as? Bool
self.serviceFixedAmount = serviceDict["serviceAmount"] as? String
aService.serviceUrl = serviceDict["serviceURL"] as! String
aService.serviceName = serviceDict["serviceName"] as! String
aService.serviceStatus = serviceDict["serviceStatus"] as? Bool
aService.serviceID = serviceID
self.serviceArray.append(aService)
self.tableView.reloadData()
})
}
}
}
})
}
}
})
})
}
}
})
正如预期的那样,因为我不再拥有firebase节点,所以它打印了#34;没有孩子&#34;但它仍然显示最后一个tableview单元格......所以
答案 0 :(得分:0)
好吧,我上次的编辑是我需要的一行代码。
而不是
self.tableView.endUpdates()
我用
替换了它self.tableView.reloadData()
所以(没有重新编写那个长方法)我只是写了另一种方法
func checkIfDataExits() {
serviceArray.removeAll()
ref.observeSingleEvent(of: .value, with: { snapshot in
if snapshot.hasChild("services") {
self.pullCardData()
} else {
self.tableView.reloadData()
}
})
}
这种方法决定是否运行那么长的
答案 1 :(得分:0)
解决:
serviceArray.removeAll()
代码:
ref.observeSingleEvent(of: .value, with: { snapshot in
if snapshot.hasChild("services") {
self.pullCardData()
} else {
//code here
serviceArray.removeAll()
self.tableView.reloadData()
}
})