Ios 通过委派在集合视图单元格中重新加载集合视图
我有一个带有集合视图的控制器(a),该集合视图具有2个单元格类。其中一个(B)包含另一个集合视图。在做了一些研究之后,我仍然不知道如何从(A)或其他地方更新(B)中的单元格以得到我想要的 问题 (B) 按下按钮时无法正确重新加载:与按钮绑定的单元格仍然可见,即使该单元格已从其委托方法(A)中的Ios 通过委派在集合视图单元格中重新加载集合视图,ios,swift,swift3,uicollectionview,uicollectionviewcell,Ios,Swift,Swift3,Uicollectionview,Uicollectionviewcell,我有一个带有集合视图的控制器(a),该集合视图具有2个单元格类。其中一个(B)包含另一个集合视图。在做了一些研究之后,我仍然不知道如何从(A)或其他地方更新(B)中的单元格以得到我想要的 问题 (B) 按下按钮时无法正确重新加载:与按钮绑定的单元格仍然可见,即使该单元格已从其委托方法(A)中的userFriendRequests数组中删除。作为奖励,当我滚动到(B)中的一个新单元格时,在cell.user=userFriendRequests[indexath.row]行中声明“索引超出范围”时
userFriendRequests
数组中删除。作为奖励,当我滚动到(B)中的一个新单元格时,在cell.user=userFriendRequests[indexath.row]
行中声明“索引超出范围”时,我会崩溃
我拥有的
财务主任(A)
PS:self.attemptReloadOfCollectionView()
是一个函数,它简单地使计时器无效,将其设置为0.1秒,然后在(a)的集合视图上调用reloadData()
采集视图单元(B)
我想要实现的目标
更新(B)当从(a)中的userFriendRequests
数组中删除User
时,该User
由(B)通过委派传递的id
标识
任何一个善良的灵魂都可能对如何解决这个问题有想法吗
提前感谢您的帮助 为了澄清,您有1个viewController和2个collectionViews?实际上是@JoséNeto。控制器(A)实现一个集合视图:如果
userFriendRequests
包含元素,则此集合视图的第一部分将注册/出列类(B)中定义的单元格。这些类(B)单元还实现了从(a)传递的userFriendRequests
数据源的集合视图。我的问题是,当我更新(A)中的userFriendRequests
并重新加载(A)的集合视图时,(B)中的集合视图没有重新加载。这触发了我在问题中描述的问题。只是为了测试,您是否尝试使用通知?当您在控制器A中执行某些操作时,您使用通知通知B,以便他可以重新加载集合视图的内容?
protocol UserFriendRequestsDelegate: class {
func didPressConfirmFriendButton(_ friendId: String?)
}
/...
fileprivate var userFriendRequests = [User]()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if userFriendRequests.isEmpty == false {
switch indexPath.section {
case 0:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: friendRequestCellId, for: indexPath) as! UserFriendRequests
cell.userFriendRequests = userFriendRequests
cell.delegate = self
return cell
case 1:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! UserFriendCell
let user = users[indexPath.row]
cell.user = user
return cell
default:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! UserFriendCell
return cell
}
}
/...
extension AddFriendsController: UserFriendRequestsDelegate {
internal func didPressConfirmFriendButton(_ friendId: String?) {
guard let uid = FIRAuth.auth()?.currentUser?.uid, let friendId = friendId else {
return
}
let userRef = FIRDatabase.database().reference().child("users_friends").child(uid).child(friendId)
let friendRef = FIRDatabase.database().reference().child("users_friends").child(friendId).child(uid)
let value = ["status": "friend"]
userRef.updateChildValues(value) { (error, ref) in
if error != nil {
return
}
friendRef.updateChildValues(value, withCompletionBlock: { (error, ref) in
if error != nil {
return
}
self.setUpRequestsStatusesToConfirmed(uid, friendId: friendId)
DispatchQueue.main.async(execute: {
let index = self.currentUserFriendRequests.index(of: friendId)
self.currentUserFriendRequests.remove(at: index!)
for user in self.userFriendRequests {
if user.id == friendId {
self.userFriendRequests.remove(at: self.userFriendRequests.index(of: user)!)
}
}
self.attemptReloadOfCollectionView()
})
})
}
}
weak var delegate: UserFriendRequestsDelegate?
var userFriendRequests = [User]()
/...
@objc fileprivate func confirmFriendButtonPressed(_ sender: UIButton) {
delegate?.didPressConfirmFriendButton(friendId)
}
/...
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return userFriendRequests.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: friendRequestCellId, for: indexPath) as! FriendRequestCell
cell.user = userFriendRequests[indexPath.row]
return cell
}
/...
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let firstName = userFriendRequests[indexPath.row].first_name, let lastName = userFriendRequests[indexPath.row].last_name, let id = userFriendRequests[indexPath.row].id else {
return
}
nameLabel.text = firstName + " " + lastName
friendId = id
confirmButton.addTarget(self, action: #selector(confirmFriendButtonPressed(_:)), for: .touchUpInside)
}