@IBAction func btnDeleteCell(_ sender: UIButton) {
let UC = UserControllers()
UC.deleteSocialPlatform(indexRow)
DispatchQueue.main.async {
self.parentCollectionView!.reloadData()
}
print(indexRow)
}
btnDeleteCell
函数在我的collectionviewcell类内部,而在创建单元格时设置了parentCollectionView
:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell: SocialPlatformCollectionViewCell
//if is first element of array
if indexPath.row == 0
{
cell = cvSocialPlatform.dequeueReusableCell(withReuseIdentifier: "DefaultCell", for: indexPath) as! SocialPlatformCollectionViewCell
cell.setupCommon("socialmedialinkgrey", "defaultCell")
cell.setupbtnInfoText("Add new link")
cell.parentCollectionView = self.cvSocialPlatform
}
else
{
cell = cvSocialPlatform.dequeueReusableCell(withReuseIdentifier: "AddedCell", for: indexPath) as! SocialPlatformCollectionViewCell
cell.setupCommon("socialmedialink", "addedCell")
cell.setupInfoTextStr(userSocialPlatform[indexPath.row])
cell.setupIndex(indexPath.row)
cell.parentCollectionView = self.cvSocialPlatform
}
return cell
}
[只要用户单击特定单元格上的删除按钮,就会删除相应的文本数组,并且在firebase上执行删除过程,但是以某种方式,集合视图不会在视图控制器中刷新。我在这里做错了吗?
删除UserControllers
中的功能:
func deleteSocialPlatform(_ indexRow: Int)
{
let userRef = dbManager.databaseRef.child("users").child(dbManager.currentUserID!)
userRef.observeSingleEvent(of: .value) { (snapshot) in
let value = snapshot.value as? NSDictionary
var socialplatformArray = value?["socialplatform"] as! Array<String>
print(socialplatformArray)
socialplatformArray.remove(at: indexRow)
let newValue = ["socialplatform": socialplatformArray]
userRef.updateChildValues(newValue)
}
这就是您的删除功能的外观
func deleteSocialPlatform(_ indexRow: Int)
{
let userRef = dbManager.databaseRef.child("users").child(dbManager.currentUserID!)
userRef.observeSingleEvent(of: .value) { (snapshot) in
let value = snapshot.value as? NSDictionary
var socialplatformArray = value?["socialplatform"] as! Array<String>
print(socialplatformArray)
socialplatformArray.remove(at: indexRow)
let newValue = ["socialplatform": socialplatformArray]
userRef.updateChildValues(newValue)
userSocialPlatform = socialplatformArray
}
func deleteSocialPlatform(_ indexRow: Int, completion:@escaping(Array<String>) -> ())
{
let userRef = dbManager.databaseRef.child("users").child(dbManager.currentUserID!)
userRef.observeSingleEvent(of: .value) { (snapshot) in
let value = snapshot.value as? NSDictionary
var socialplatformArray = value?["socialplatform"] as! Array<String>
print(socialplatformArray)
socialplatformArray.remove(at: indexRow)
let newValue = ["socialplatform": socialplatformArray]
userRef.updateChildValues(newValue)
completion(socialplatformArray)
}
}
@IBAction func btnDeleteCell(_ sender: UIButton) {
let UC = UserControllers()
let parentViewController = PatientPersonalInfoViewController()
UC.deleteSocialPlatform(indexRow)
{
(socialplatformArray) in
parentViewController.userSocialPlatform = socialplatformArray
}
DispatchQueue.main.async {
self.parentCollectionView!.reloadData()
}
print(indexRow)
}