我有一个 ProfileView 控制器,我在其中显示登录用户的电子邮件。当我注销并再次登录时,电子邮件会更新,但新信息时不会更新 tableview。即使完成,重新加载也只是将重复的行附加到视图中。下面是我的代码。有人可以检查我可能做错了什么
import UIKit
import FirebaseAuth
import SDWebImage
import FirebaseDatabase
final class ProfileViewController: UIViewController {
@IBOutlet var tableView: UITableView!
var data = [ProfileViewModel]()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let email = Auth.auth().currentUser?.email! else {
return
}
tableView.register(ProfileTableViewCell.self,
forCellReuseIdentifier: ProfileTableViewCell.identifier)
data.append(ProfileViewModel(viewModelType: .info,
title: "Email: \(email ?? "No Email")",
handler: nil))
tableView.register(UITableViewCell.self,
forCellReuseIdentifier: "cell")
tableView.delegate = self
tableView.dataSource = self
tableView.tableHeaderView = createTableHeader()
}
func createTableHeader() -> UIView? {
guard let email = UserDefaults.standard.value(forKey: "email") as? String else {
return nil
}
let safeEmail = DatabaseManager.safeEmail(emailAddress: email)
let filename = safeEmail + "_profile_picture.png"
let path = "images/"+filename
let headerView = UIView(frame: CGRect(x: 0,
y: 0,
width: self.view.width,
height: 300))
headerView.backgroundColor = .link
let imageView = UIImageView(frame: CGRect(x: (headerView.width-150) / 2,
y: 75,
width: 150,
height: 150))
imageView.contentMode = .scaleAspectFill
imageView.backgroundColor = .white
imageView.layer.borderColor = UIColor.white.cgColor
imageView.layer.borderWidth = 3
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = imageView.width/2
headerView.addSubview(imageView)
StorageManager.shared.downloadURL(for: path, completion: { result in
switch result {
case .success(let url):
imageView.sd_setImage(with: url, completed: nil)
case .failure(let error):
print("Failed to get download url: \(error)")
}
})
return headerView
}
}
extension ProfileViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let viewModel = data[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: ProfileTableViewCell.identifier,
for: indexPath) as! ProfileTableViewCell
cell.setUp(with: viewModel)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
data[indexPath.row].handler?()
}
}
class ProfileTableViewCell: UITableViewCell {
static let identifier = "ProfileTableViewCell"
public func setUp(with viewModel: ProfileViewModel) {
self.textLabel?.text = viewModel.title
switch viewModel.viewModelType {
case .info:
textLabel?.textAlignment = .center
selectionStyle = .none
}
}
}
答案 0 :(得分:0)
我认为您需要先清空数据数组,然后再将新对象添加到该数组中。
写就好了
data.removeAll()
之前
data.append(ProfileViewModel(viewModelType: .info, title: "Email: \(email ?? "No Email")", handler: nil))
。
我认为这将解决您的问题。