数据源正在更新,但更改未在Tableview中显示。
我正在试验“朋友,用户和用户请求”表视图,其中包括3个部分。当用户收到用户的请求时,他或她将出现在请求部分下方。但是,当用户单击“接受”时,控制台读取的是Firebase服务器,但该请求实际进行的更改未注册,并且该请求保留在该部分中,直到您重新加载视图为止。我试图强制重新加载表格视图,但无济于事。
struct SectionOfuserRxx {
var header: String
var items: [Item]
}
extension SectionOfuserRxx: IdentifiableType {
}
extension SectionOfuserRxx: AnimatableSectionModelType {
// typealias Item = user
typealias Identity = String
////
typealias Item = user
init(original: SectionOfuserRxx, items: [Item]) {
self = original
self.items = items
}
var identity: String {
return header
}
}
// Above viewDidLoad
let disposeBag = DisposeBag()
//
//Inside viewDidLoad
let dataSource = RxTableViewSectionedAnimatedDataSource<SectionOfuserRxx>(
configureCell: { dataSource, tableView, indexPath, item in
switch (indexPath.section) {
case 2:
let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as? userCell
let userProfileImageURLFirebaseReturn = item.userImageURL
let userNameTagFirebaseReturn = item.nametag
let userNameFirebaseReturn = item.username
let imageURL = userProfileImageURLFirebaseReturn
let resource = ImageResource(downloadURL: URL(string: imageURL!)!, cacheKey: userProfileImageURLFirebaseReturn)
cell?.userProfileImage.kf.setImage(with: resource)
cell?.usernameLbl.text = userNameFirebaseReturn
cell?.userNameTagLbl.text = "| " + userNameTagFirebaseReturn!
cell!.setFunction {
cell!.button.isEnabled = true
let id = item.userUID
FriendSystem.system.sendRequestToUser(id!)
}
return cell!
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "friendsCell", for: indexPath) as? friendsCell
cell!.buttons.isEnabled = true
cell!.buttons.setTitle("Remove", for: UIControl.State())
// print("Friend Count: \(item.count)");
print(" INDEXING ROW FOR FRIENDS: \(indexPath.row)")
let userProfileImageURLFirebaseReturn = item.userImageURL ?? ""
let userNameTagFirebaseReturn = item.nametag ?? ""
let userNameFirebaseReturn = item.username ?? ""
let imageURL = userProfileImageURLFirebaseReturn
// self.urlContentArray.append(imageURL)
if imageURL != "" {
let resource = ImageResource(downloadURL: URL(string: imageURL)!, cacheKey: userProfileImageURLFirebaseReturn)
cell?.userProfileImage.kf.setImage(with: resource)
} else {
cell?.userProfileImage.image = UIImage(named: "heartfilled")
}
cell?.usernameLbl.text = userNameFirebaseReturn
cell?.userNameTagLbl.text = userNameTagFirebaseReturn;
cell!.setFunction {
cell!.buttons.isEnabled = true
let id = item.userUID
// FriendSystem.system.friendList.remove(at: indexPath.row)
FriendSystem.system.removeFriend(id!)
}
return cell!
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "requestCell", for: indexPath) as? requestCell
cell!.button.isEnabled = true
// cell!.button.setTitle("Accept", for: UIControl.State())
let userProfileImageURLFirebaseReturn = item.userImageURL
let userNameTagFirebaseReturn = item.nametag
let userNameFirebaseReturn = item.username
let imageURL = userProfileImageURLFirebaseReturn
// self.urlContentArray.append(imageURL)
// guard imageURL != nil && imageURL != "" else {return}
if imageURL != nil && userProfileImageURLFirebaseReturn != nil && userProfileImageURLFirebaseReturn != "" && imageURL != "" {
let resource = ImageResource(downloadURL: URL(string: imageURL!)!, cacheKey: userProfileImageURLFirebaseReturn)
cell?.userProfileImage.kf.setImage(with: resource)
cell?.usernameLbl.text = userNameFirebaseReturn
cell?.userNameTagLbl.text = userNameTagFirebaseReturn
};
// cell?.userProfileImage.kf.setImage(with: resource)
cell?.usernameLbl.text = userNameFirebaseReturn
cell?.userNameTagLbl.text = userNameTagFirebaseReturn
//cell?.button.backgroundColor = #colorLiteral(red: 1, green: 0.3354814752, blue: 0.289293822, alpha: 1)
cell!.setFunction {
cell!.button.isEnabled = true
let id = item.userUID
FriendSystem.system.acceptFriendRequest(id!)
}
return cell!
default:
return UITableViewCell();
}},
titleForHeaderInSection: { dataSource, index in
return dataSource.sectionModels[index].header
});
dataSource.animationConfiguration = AnimationConfiguration(insertAnimation: .fade, reloadAnimation: .fade, deleteAnimation: .fade);
*******这是如何更新视图并在VIEWDIDLOAD中使用FIREBASE观察器推送数组的方法*******
var requestList =用户
var requestListUser = [String]()
/** Adds a friend request observer. The completion function will run every time this list changes, allowing you
to update your UI. */
func addRequestObserver(_ update: @escaping () -> Void) {
let ref = USER_REF.child((Auth.auth().currentUser?.uid)!).child("requests");
ref.keepSynced(true);
ref.observe(DataEventType.value, with: { (snapshot) in
guard (Auth.auth().currentUser?.uid) != nil else { return }
self.requestList.removeAll()
self.requestListUser.removeAll()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let id = child.key
FriendSystem.system.getCurrentUser({ (userDataReturned) in
if userDataReturned.email != nil && userDataReturned.userImageURL != nil &&
userDataReturned.nametag != nil &&
userDataReturned.username != nil &&
userDataReturned.userUID != nil {
self.requestListUser.append(id);
self.getUser(id, completion: { (user) in
self.requestList.append(user)
update()
});
} else {
print("User request account had an account not completely populated")
}
})
// self.requestListUser.append(id)
// self.getUser(id, completion: { (user) in
// self.requestList.append(user)
// update()
// })
}
// If there are no children, run completion here instead
if snapshot.childrenCount == 0 {
update()
}
})
}
****在VIEWDIDLOAD中。 ****
// THIS添加一个观察者并填充userList数组 FriendSystem.system.addUserObserver {
}