swift - 集合视图单元格中的 IndexRow 变量在 reloadData() 后不会更改
问题描述
class SocialPlatformCollectionViewCell: UICollectionViewCell {
//added cell
@IBOutlet weak var btnDelete: UIButton!
//variables
var indexRow: Int = 0
var parentCollectionView: UICollectionView?
func setupIndex(_ indexRow: Int)
{
self.indexRow = indexRow
}
@IBAction func btnDeleteCell(_ sender: UIButton) {
let UC = UserControllers()
UC.deleteSocialPlatform(indexRow)
{
(socialplatformArray) in
//use the linker to access to PatientInfoPersonalViewController elements
ViewControllersLinkers.patientInfoVC?.userSocialPlatform = socialplatformArray
let indexPath = self.parentCollectionView?.indexPath(for: self)
self.parentCollectionView?.deleteItems(at: [indexPath!])
print("this cell index is \(self.indexRow)")
DispatchQueue.main.async {
self.parentCollectionView?.reloadData()
}
}
}
}
这是collectionviewcell class
我用来在集合视图中生成单元格的
class PatientPersonalInfoViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var cvSocialPlatform: UICollectionView!
//the value for this array will be generated from the previous view controller using segue
var userSocialPlatform: Array<String> = []
override func viewDidLoad() {
super.viewDidLoad()
//set delegate for collection view
self.cvSocialPlatform.delegate = self
self.cvSocialPlatform.dataSource = self
//set the linker
ViewControllersLinkers.patientInfoVC = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return userSocialPlatform.count
}
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.parentCollectionView = self.cvSocialPlatform
}
else
{
cell = cvSocialPlatform.dequeueReusableCell(withReuseIdentifier: "AddedCell", for: indexPath)as! SocialPlatformCollectionViewCell
cell.setupIndex(indexPath.row)
cell.parentCollectionView = self.cvSocialPlatform
}
return cell
}
}
这是我的ViewCollection
课(用户界面)
class UserControllers
{
let dbManager = DatabaseManager()
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("This cell index: \(indexRow)")
socialplatformArray.remove(at: indexRow)
let newValue = ["socialplatform": socialplatformArray]
userRef.updateChildValues(newValue)
completion(socialplatformArray)
}
}
}
这是我UserControllers
在用户单击单元格中的删除按钮时处理删除请求
但是由于某种原因,每当我删除一行时,即使在 reloadData 之后,每个单元格的索引号也不会改变,但是如果我删除该deleteItems(at:)
行,索引号确实会改变,一切都很好。我想要被删除的动画。我不知道为什么会这样。例如,如果我选择了第二个单元格,索引号是 1,然后在它被删除后,向上移动的第三个单元格的索引号仍然是 2 而不是 1。如果我删除该deleteItems(at:)
行,则不会发生这种情况
解决方案
当您调用以下行时,您可能正在创建 UserControllers() 的新实例。
让 UC = UserControllers()
相反,尝试直接通过 UC...
var userCont: UserControllers!
func setupIndex(_ indexRow: Int, _ userControllers: UserControllers) {
self.indexRow = indexRow
self.userCont = userControllers
}
然后在删除函数中,调用userCont.deleteSocialPlatform...
否则,我会查看这个链接,该链接介绍了如何有效地删除 tableview 中的行。
让我知道这是否有效?
推荐阅读
- sql - 基于现有表字段创建 SQL 语句
- python - zeep 客户端异常。zeep.transport.session.cookies 不传播
- powershell - 是否可以检查 .cmd 运行的 powershell 脚本?
- python - 除了 subprocess.Open 之外,有没有办法从 python 调用“hadoop cp/distcp”?
- c# - C# Linq - 对已经分组的数据进行分组
- javascript - 如何在打字稿中访问geoJson坐标数组?
- vue.js - 路由器视图中的路由器视图
- c# - 使用 Firebase Auth 对其进行身份验证后,如何在最终用户 c# 应用程序中安全地使用 Google Speech API?
- python - 用 dict 值替换列表值中的列表
- c# - 无法使用本机 .SO 创建 Java 库绑定