图像在 TableView 的数据搜索结果中表现不准确

Image not behaving accurately in data search results on TableView

我有来自 Firebase 的数据,加载数据时,我希望根据自定义单元格中的某些逻辑隐藏或显示图像。当数据没有被过滤时,它工作得很好,但是当我在搜索栏中输入第二个或将范围栏更改为不同的索引时,图像表现不正确。 例如:索引 0 不应该有图像而索引 1 应该有。这是它首次加载时的显示方式。但是,当我搜索时,我知道之前的索引 1(现在是索引 0)应该仍然有它的图像,但事实并非如此。但是,如果我单击转到详细信息控制器,它会将我带到正确的页面。就像它加载了所有准确的信息,但在原始索引 0 上执行逻辑。我希望得到一些帮助,因为我一直在寻找答案。提前致谢!

表格视图单元格:

class SearchTalentCell: UITableViewCell {
@IBOutlet weak var userProfileImage: UIImageView!
@IBOutlet weak var talentUserName: UILabel!
@IBOutlet weak var selectedImg: UIImageView!
@IBOutlet weak var inviteSentImg: UIImageView!
var prospectRef: FIRDatabaseReference!
//@IBOutlet weak var radioButton: UIButton!
var currentTalent: UserType!
//var delegate: SearchCellDelegate?

func setTalent(talent: UserType) {
    currentTalent = talent
    currentTalent.userKey = talent.userKey
}

override func awakeFromNib() {
    super.awakeFromNib()
    let tap = UITapGestureRecognizer(target: self, action: #selector(selectTapped))
    tap.numberOfTapsRequired = 1
    selectedImg.addGestureRecognizer(tap)
    selectedImg.isUserInteractionEnabled = true
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

/*@IBAction func radioButtonTapped(_ sender: Any) {
    delegate?.didTapRadioButton(userKey: currntTalent.userKey, searchSelected: currntTalent.searchSelected!.rawValue, radioButton: radioButton)
}*/


func configureCell(user: UserType, img: UIImage? = nil) {
    prospectRef = Cast.REF_PRE_PRODUCTION_CASTING_POSITION.child(ProjectDetailVC.currentProject).child(FIRDataCast.prospect.rawValue).child(CastingDetailVC.positionName).child(user.userKey)
    //setTalent(talent: user)
    self.talentUserName.text = "\(user.firstName) \(user.lastName)"
    //self.inviteSentImg.image = UIImage(named: "inviteSent")
    //user.adjustSearchSelected(talent: user, radioButton: radioButton)
    prospectRef.observeSingleEvent(of: .value, with: { (snapshot) in
        if let _ = snapshot.value as? NSNull {
            self.inviteSentImg.isHidden = true
            print("**Image hidden")
        } else {
            self.inviteSentImg.image = UIImage(named: "inviteSent")
            print("**Image shown")
        }
    })

    //Image Caching
    if img != nil {
        self.userProfileImage.image = img
    } else {

        if let imageURL = user.profileImage {

            let ref = FIRStorage.storage().reference(forURL: imageURL)
            ref.data(withMaxSize: 2 * 1024 * 1024, completion: { (data, error) in
                if error != nil {
                    print("ZACK: Unable to download image from Firebase Storage")
                } else {
                    print("ZACK: Image downloaded from Firebase Storage")
                    if let imgData = data {
                        if let img = UIImage(data: imgData) {
                            self.userProfileImage.image = img
                            SearchTalentVC.userProfileImageCache.setObject(img, forKey: imageURL as NSString)
                        }
                    }
                }
            })
        }
    }



}

Viewcontroller:

class SearchTalentVC: UITableViewController/*, SearchCellDelegate*/ {

var searchingRole = [Cast]()
var unfilteredTalent = [UserType]()
var filteredTalent = [UserType]()
var selectedTalent = [UserType]()
var matchingTalentUserKeys = [String]()
var isFiltered = false
var prospectRef: FIRDatabaseReference!
static var userProfileImageCache: NSCache<NSString, UIImage> = NSCache()
let searchController = UISearchController(searchResultsController: nil)

//@IBOutlet weak var searchBar: UISearchBar!

override func viewDidLoad() {
    super.viewDidLoad()
    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search Talent"
    searchController.searchBar.barStyle = .black
    navigationItem.searchController = searchController
    definesPresentationContext = true
    searchController.searchBar.scopeButtonTitles = ["All", "Role Specific"]
    searchController.searchBar.tintColor = UIColor.white
    searchController.searchBar.delegate = self
    searchController.searchResultsUpdater = self


    getTalentProfiles()

}

func searchBarIsEmpty() -> Bool {
    return searchController.searchBar.text?.isEmpty ?? true
}

func filterContentForSearchText(_ searchText: String, scope: String = "All") {
    filteredTalent = unfilteredTalent.filter({ (talent : UserType) -> Bool in
        let doesTalentMatch = (scope == "All") || doesUserKeyMatch(talent: talent.userKey)

        if searchBarIsEmpty() {
            return doesTalentMatch
        } else {

            let fullName = "\(talent.firstName) \(talent.lastName)"
            return doesTalentMatch && fullName.lowercased().contains(searchText.lowercased())
        }
    })
    tableView.reloadData()
}



func doesUserKeyMatch(talent: String) -> Bool {
    self.filterRoleFeature()

    return matchingTalentUserKeys.contains(talent)
}

func isSearching() -> Bool {
    let searchBarScopeIsFiltering = searchController.searchBar.selectedScopeButtonIndex != 0
    return searchController.isActive && (!searchBarIsEmpty() || searchBarScopeIsFiltering)
}


// 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

    if isSearching() {
        return filteredTalent.count
    } else {
        return unfilteredTalent.count
    }

}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cellIdentifier = "userSearchCell"
    if let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? SearchTalentCell {
        var talent: UserType
        if isSearching() {
            print("we are searching")
            talent = self.filteredTalent[indexPath.row]

            print("indexPath: \(indexPath.row)")
        } else {
            print("we are not searching")
            talent = self.unfilteredTalent[indexPath.row]

        }

        if let imageURL = talent.profileImage {
            if let img = SearchTalentVC.userProfileImageCache.object(forKey: imageURL as NSString) {
                cell.configureCell(user: talent, img: img)

            } else {
                cell.configureCell(user: talent)

                //cell.delegate = self
            }
            return cell
        } else {
            cell.configureCell(user: talent)
            //cell.delegate = self
            return SearchTalentCell()
        }
    } else {
        return SearchTalentCell()
    }
}

extension SearchTalentVC: UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {
        let searchBar = searchController.searchBar
        let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
        filterContentForSearchText(searchController.searchBar.text!, scope: scope)
        self.tableView.reloadData()
    }


}


extension SearchTalentVC: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
        filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
    }
}

如果您在 If 部分隐藏图像,那么您应该将显示图像的逻辑也放在 else 块中。看看这是否能解决您的问题。

if let _ = snapshot.value as? NSNull {
    self.inviteSentImg.isHidden = true
    print("**Image hidden")
} else {
    self.inviteSentImg.isHidden = false
    self.inviteSentImg.image = UIImage(named: "inviteSent")
    print("**Image shown")
}