简体   繁体   English

滚动时Swift UITableViewCell按钮状态更改

[英]Swift UITableViewCell Button State Change on Scroll

So I have a button in my UITableViewCell that I change the state and update the database. 因此,我的UITableViewCell中有一个按钮,可以更改状态并更新数据库。 However, when I scroll and go back the state is in the original state as when the view was loaded. 但是,当我滚动并返回时,状态与加载视图时一样处于原始状态。

How do I get the state to stay changed after scrolling? 滚动后如何使状态保持不变?

I tried setting the state images in prepareForReuse and that did not work. 我尝试在prepareForReuse中设置状态图像,但是这没有用。

// MARK: - Outlets
@IBOutlet weak var locationImage: UIImageView!
@IBOutlet weak var displayName: UILabel!
@IBOutlet weak var countryLabel: UILabel!
@IBOutlet weak var beenHereLabel: SpringLabel!
@IBOutlet weak var needToGoLabel: SpringLabel!
@IBOutlet weak var wavetrotterButton: SpringButton!
@IBOutlet weak var checkmarkButton: SpringButton!

// MARK: - Variables
var db:Firestore!
let selection = UISelectionFeedbackGenerator()
let notification = UINotificationFeedbackGenerator()
var documentId:String!

// MARK: - Nib shown
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    db = Firestore.firestore()

}

func customInit(displayName: String, id: String, country: String, image: UIImage) {
    self.displayName.text = displayName
    self.documentId = id
    self.countryLabel.text = country
    self.locationImage.image = image
}

// MARK: - Actions
@IBAction func checkmarkButtonPressed(_ sender: UIButton) {
    notification.notificationOccurred(.success)
    checkmarkButton.animation = "pop"
    beenHereLabel.animation = "pop"
    if checkmarkButton.isSelected == true {
        checkmarkButton.animate()
        beenHereLabel.animate()
        checkmarkButton.isSelected = false
        // Delete location surfed
        if let user = Auth.auth().currentUser {
            Firestore.firestore().collection("users").document(user.uid).collection("surfed").document("\(documentId!)").delete() { err in
                if let err = err {
                    print("Error removing document: \(err)")
                } else {
                    print("\(self.documentId!) successfully removed!")
                }

            }
        }
    } else {
        checkmarkButton.animate()
        beenHereLabel.animate()
        checkmarkButton.isSelected = true
        // Add location surfed
        if let user = Auth.auth().currentUser {
            Firestore.firestore().collection("users").document(user.uid).collection("surfed").document("\(documentId!)").setData([
                "name":displayName.text ?? "",
                "country":countryLabel.text ?? ""
            ])  { err in
                if let err = err {
                     print("Error writing document: \(err)")
                } else {
                    print("\(self.documentId!) added to surfed locations")
                }
            }
        } 
    }
}

@IBAction func wavetrotterButtonPressed(_ sender: UIButton) {
    notification.notificationOccurred(.success)
    wavetrotterButton.animation = "pop"
    needToGoLabel.animation = "pop"
    if wavetrotterButton.isSelected == true {
        wavetrotterButton.animate()
        needToGoLabel.animate()
        wavetrotterButton.isSelected = false
        // Delete location wantToSurf
        if let user = Auth.auth().currentUser {
            Firestore.firestore().collection("users").document(user.uid).collection("wantToSurf").document("\(documentId!)").delete() { err in
                if let err = err {
                    print("Error removing document: \(err)")
                } else {
                    print("\(self.documentId!) successfully removed!")
                }
            }
        }
    } else {
        wavetrotterButton.animate()
        needToGoLabel.animate()
        wavetrotterButton.isSelected = true
        // Add location wantToSurf
        if let user = Auth.auth().currentUser {
            Firestore.firestore().collection("users").document(user.uid).collection("wantToSurf").document("\(documentId!)").setData([
                "name":displayName.text ?? "",
                "country":countryLabel.text ?? ""
            ])  { err in
                if let err = err {
                    print("Error writing document: \(err)")
                } else {
                    print("\(self.documentId!) added to surfed locations")
                }
            }
        }
    }
}

LocationResultsTableViewController.swift LocationResultsTableViewController.swift

   // MARK: - Variables
var listName: String?
var listId: String?
var db:Firestore!
let storage = Storage.storage().reference()
var locationArray = [Location]()
var userSurfedArray = [String]()
var userWantToSurfArray = [String]()
let pullToRefreshControl = UIRefreshControl()
var selectedDocumentId: String?

// MARK: - View Did Load
override func viewDidLoad() {
    super.viewDidLoad()

    self.title = listName

    db = Firestore.firestore()

    SVProgressHUD.show()

    getListLocations()
    getUserSurfedArray()
    getUserWantToSurfArray()

    // Configure the cell to the nib file
    let nib = UINib(nibName: "LocationCell", bundle: nil)
    tableView.register(nib, forCellReuseIdentifier: "locationCell")

    self.refreshControl = pullToRefreshControl
    pullToRefreshControl.addTarget(self, action: #selector(refreshTable), for: .valueChanged)

    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: nil, action: nil)

}

// MARK: - View Will Appear
override func viewWillAppear(_ animated: Bool) {
    tableView.reloadData()
}

// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return locationArray.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath) as! LocationCell
    // Configure the cell...

    let location = locationArray[indexPath.row]

    cell.documentId = location.documentId

    // Set button states
    if self.userSurfedArray.contains(cell.documentId!) {
        cell.checkmarkButton.isSelected = true
    } else {
        cell.checkmarkButton.isSelected = false
    }

    if self.userWantToSurfArray.contains(cell.documentId!) {
        cell.wavetrotterButton.isSelected = true
    } else {
        cell.wavetrotterButton.isSelected = false
    }

    let locationImageRef = storage.child("locationImages/"+(location.documentId)+".jpg")
    // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
    locationImageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
        if let error = error {
            // Uh-oh, an error occurred! Display Default image
            print("Error - unable to download image: \(error)")
        } else {
            // Data for "locationImages/(locationId).jpg" is returned
            cell.customInit(displayName: location.name, id: location.documentId, country: location.country, image: UIImage(data: data!)!)
        }
        SVProgressHUD.dismiss()
    }

    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedDocumentId = locationArray[indexPath.row].documentId
    self.performSegue(withIdentifier: "goToLocationProfileSegue", sender: self)
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 260
}


// MARK: - Functions
func getListLocations() {
    if Auth.auth().currentUser != nil {
        db.collection("locations").whereField("lists."+listId!, isEqualTo: true).getDocuments() { (querySnapshot, error) in
            if let error = error {
                print("Error getting documents: \(error)")
            } else {
                print(querySnapshot?.documents.count ?? "0")
                for document in querySnapshot!.documents {
                    self.locationArray.append(Location(documentId: document.documentID, name: document["name"] as! String, country: document["country"] as! String))
                }
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
        }
    }
}

func getUserSurfedArray() {
    if let user = Auth.auth().currentUser {
        db.collection("users").document(user.uid).collection("surfed").getDocuments() { (querySnapshot, error) in
            if let error = error {
                print("Error getting documents: \(error)")
            } else {
                for document in querySnapshot!.documents {
                    self.userSurfedArray.append(document.documentID)
                }
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
        }
    }
}

func getUserWantToSurfArray() {
    if let user = Auth.auth().currentUser {
        db.collection("users").document(user.uid).collection("wantToSurf").getDocuments() { (querySnapshot, error) in
            if let error = error {
                print("Error getting documents: \(error)")
            } else {
                for document in querySnapshot!.documents {
                    self.userWantToSurfArray.append(document.documentID)
                }
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
        }
    }
}

其背后的原因是单元重用,您必须将按钮的状态保存在该indexPath处,并将其还原到cellForRowAt

As I can see from your code, you are loading the buttons from below lines: 正如我从您的代码中看到的那样,您正在从下面的行中加载按钮:

In cellForRowAt indexPath cellForRowAt indexPath

// Set button states
if self.userSurfedArray.contains(cell.documentId!) {
    cell.checkmarkButton.isSelected = true
} else {
    cell.checkmarkButton.isSelected = false
}

if self.userWantToSurfArray.contains(cell.documentId!) {
    cell.wavetrotterButton.isSelected = true
} else {
    cell.wavetrotterButton.isSelected = false
}

The thing is on scrolling, when table cell hide it get dequeued and when it appears again this cell reloads with dequeueReusableCell(withIdentifier:) and you are not updating the userSurfedArray & userWantToSurfArray on clicking the buttons. 事情在滚动,当表单元格隐藏它出队时,再次出现时,该单元格用dequeueReusableCell(withIdentifier:)重新加载,并且您没有在单击按钮时更新userSurfedArrayuserWantToSurfArray

You need to update the userSurfedArray & userWantToSurfArray on button click. 您需要在单击按钮时更新userSurfedArrayuserWantToSurfArray

It it seems insufficient, please post your code where you are loading these arrays so that I can help you to explain how to update these. 似乎还不够,请在加载这些数组的地方张贴代码,以便我帮助您解释如何更新它们。


Update: 更新:

In your updated code, I can see you are loading the userSurfedArray & userWantToSurfArray in viewDidLoad() only. 在更新的代码中,我可以看到您仅在viewDidLoad()中加载userSurfedArrayuserWantToSurfArray

To update userSurfedArray & userWantToSurfArray : Create a delegate in UITableViewCell and conform this protocol in LocationResultsTableViewController like following: 更新userSurfedArrayuserWantToSurfArray :在UITableViewCell中创建一个委托,并在LocationResultsTableViewController遵循此协议,如下所示:

In UITableViewCell: (Supposing table cell's name is LocTableViewCell ) 在UITableViewCell中:(假设表单元格的名称为LocTableViewCell

import UIKit

protocol UpdateUserArrayDelegate: class {
    func updateUserSurfedArray(documentId: String, isAdd: Bool)
    func updateUserWantToSurfArray(documentId: String, isAdd: Bool)
}

class LocTableViewCell: UITableViewCell {

weak var cellDelegate: UpdateUserArrayDelegate?

// MARK: - Outlets
@IBOutlet weak var locationImage: UIImageView!
@IBOutlet weak var displayName: UILabel!
@IBOutlet weak var countryLabel: UILabel!
@IBOutlet weak var beenHereLabel: SpringLabel!
@IBOutlet weak var needToGoLabel: SpringLabel!
@IBOutlet weak var wavetrotterButton: SpringButton!
@IBOutlet weak var checkmarkButton: SpringButton!

// MARK: - Variables
var db:Firestore!
let selection = UISelectionFeedbackGenerator()
let notification = UINotificationFeedbackGenerator()
var documentId:String!

// MARK: - Nib shown
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    db = Firestore.firestore()

}

func customInit(displayName: String, id: String, country: String, image: UIImage) {
    self.displayName.text = displayName
    self.documentId = id
    self.countryLabel.text = country
    self.locationImage.image = image
}

// MARK: - Actions
@IBAction func checkmarkButtonPressed(_ sender: UIButton) {
    notification.notificationOccurred(.success)
    checkmarkButton.animation = "pop"
    beenHereLabel.animation = "pop"
    if checkmarkButton.isSelected == true {
        checkmarkButton.animate()
        beenHereLabel.animate()
        checkmarkButton.isSelected = false
        // Delete location surfed
        if let user = Auth.auth().currentUser {
            Firestore.firestore().collection("users").document(user.uid).collection("surfed").document("\(documentId!)").delete() { err in
                if let err = err {
                    print("Error removing document: \(err)")
                } else {
                    cellDelegate?.updateUserSurfedArray(documentId: self.documentId, isAdd: false)
                    print("\(self.documentId!) successfully removed!")
                }

            }
        }
    } else {
        checkmarkButton.animate()
        beenHereLabel.animate()
        checkmarkButton.isSelected = true
        // Add location surfed
        if let user = Auth.auth().currentUser {
            Firestore.firestore().collection("users").document(user.uid).collection("surfed").document("\(documentId!)").setData([
                "name":displayName.text ?? "",
                "country":countryLabel.text ?? ""
            ])  { err in
                if let err = err {
                    print("Error writing document: \(err)")
                } else {
                    cellDelegate?.updateUserSurfedArray(documentId: self.documentId, isAdd: true)
                    print("\(self.documentId!) added to surfed locations")
                }
            }
        }
    }
}

@IBAction func wavetrotterButtonPressed(_ sender: UIButton) {
    notification.notificationOccurred(.success)
    wavetrotterButton.animation = "pop"
    needToGoLabel.animation = "pop"
    if wavetrotterButton.isSelected == true {
        wavetrotterButton.animate()
        needToGoLabel.animate()
        wavetrotterButton.isSelected = false
        // Delete location wantToSurf
        if let user = Auth.auth().currentUser {
            Firestore.firestore().collection("users").document(user.uid).collection("wantToSurf").document("\(documentId!)").delete() { err in
                if let err = err {
                    print("Error removing document: \(err)")
                } else {
                    cellDelegate?.updateUserWantToSurfArray(documentId: self.documentId, isAdd: false)
                    print("\(self.documentId!) successfully removed!")
                }
            }
        }
    } else {
        wavetrotterButton.animate()
        needToGoLabel.animate()
        wavetrotterButton.isSelected = true
        // Add location wantToSurf
        if let user = Auth.auth().currentUser {
            Firestore.firestore().collection("users").document(user.uid).collection("wantToSurf").document("\(documentId!)").setData([
                "name":displayName.text ?? "",
                "country":countryLabel.text ?? ""
            ])  { err in
                if let err = err {
                    print("Error writing document: \(err)")
                } else {
                    cellDelegate?.updateUserWantToSurfArray(documentId: self.documentId, isAdd: true)
                    print("\(self.documentId!) added to surfed locations")
                }
            }
        }
    }
}
}

In LocationResultsTableViewController 在LocationResultsTableViewController中

Add an extension with protocol methods 使用协议方法添加扩展

 extension LocationResultsTableViewController: UpdateUserArrayDelegate {
        func updateUserSurfedArray(documentId: String, isAdd: Bool) {
            if isAdd {
                self.userSurfedArray.append(documentId)
            } else {
                if self.userSurfedArray.contains(documentId) {
                    self.userSurfedArray.remove(at: self.userSurfedArray.index(of: documentId)!)
                }
            }
        }

    func updateUserWantToSurfArray(documentId: String, isAdd: Bool) {
        if isAdd {
            self.userWantToSurfArray.append(documentId)
        } else {
            if self.userWantToSurfArray.contains(documentId) {
                self.userWantToSurfArray.remove(at: self.userWantToSurfArray.index(of: documentId)!)
            }
        }
    }
}

Update the cellForRowAt indexPath as: (Add cell.cellDelegate = self) cellForRowAt indexPath更新为:(添加cell.cellDelegate = self)

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath) as! LocationCell
    // Configure the cell...

    let location = locationArray[indexPath.row]

    cell.documentId = location.documentId

    // Conform table cell delegate here
    cell.cellDelegate = self

    // Set button states
    if self.userSurfedArray.contains(cell.documentId!) {
        cell.checkmarkButton.isSelected = true
    } else {
        cell.checkmarkButton.isSelected = false
    }

    if self.userWantToSurfArray.contains(cell.documentId!) {
        cell.wavetrotterButton.isSelected = true
    } else {
        cell.wavetrotterButton.isSelected = false
    }

    let locationImageRef = storage.child("locationImages/"+(location.documentId)+".jpg")
    // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
    locationImageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
        if let error = error {
            // Uh-oh, an error occurred! Display Default image
            print("Error - unable to download image: \(error)")
        } else {
            // Data for "locationImages/(locationId).jpg" is returned
            cell.customInit(displayName: location.name, id: location.documentId, country: location.country, image: UIImage(data: data!)!)
        }
        SVProgressHUD.dismiss()
    }

    return cell
}

Update 更新资料

For case isAdd , we do not need to check self.userSurfedArray.contains(documentId) : 对于isAdd ,我们不需要检查self.userSurfedArray.contains(documentId)

extension LocationResultsTableViewController: UpdateUserArrayDelegate {
    func updateUserSurfedArray(documentId: String, isAdd: Bool) {
        if isAdd {
            self.userSurfedArray.append(documentId)
        } else {
            if self.userSurfedArray.contains(documentId) {
                self.userSurfedArray.remove(at: self.userSurfedArray.index(of: documentId)!)
            }
        }
    }

    func updateUserWantToSurfArray(documentId: String, isAdd: Bool) {
        if isAdd {
            self.userWantToSurfArray.append(documentId)
        } else {
            if self.userWantToSurfArray.contains(documentId) {
                self.userWantToSurfArray.remove(at: self.userWantToSurfArray.index(of: documentId)!)
            }
        }
    }
}

您需要更新数据源并替换最新数据以保留状态,否则您的tableviewcell可以重用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM