简体   繁体   English

在TableView上的数据搜索结果中图像无法正确显示

[英]Image not behaving accurately in data search results on TableView

I have data coming from Firebase and when the data is loaded I either want the an image to be hidden or shown based on some logic in my custom cell. 我有来自Firebase的数据,加载数据时,我希望隐藏图像或根据自定义单元格中的某些逻辑显示图像。 It works perfectly fine when the data isn't being filtered but the second I type in the search bar or change the scope bar to a different index the image doesn't behave right. 当不对数据进行过滤但第二次我在搜索栏中键入或将范围栏更改为图像无法正常工作的其他索引时,它的效果就很好。 For example: Index 0 should not have the image and index 1 should. 例如:索引0不应该包含图像,索引1应该具有图像。 Which is how it displays when it first loads. 首次加载时显示的方式。 However, when I search I know the previous index 1 (now index 0) should still have it's image but it doesn't. 但是,当我搜索时,我知道先前的索引1(现在为索引0)应该仍然具有它的图像,但没有。 BUT if I click to go to the detail controller it brings me to the right page. 但是,如果我单击以转到详细信息控制器,它将带我到正确的页面。 It's like it loads all the accurate info but does the logic on the original index 0. I would love some help as I have been searching for an answer FOREVER. 就像它加载了所有准确的信息,但是对原始索引0进行了逻辑处理。由于我一直在寻找答案,因此我希望获得一些帮助。 Thank you in advance! 先感谢您!

tableViewCell: tableViewCell:

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 you are hidding image in If part then you should put logic of showing image also in else block. 如果您在If部分中隐藏图像,则还应在else块中放置显示图像的逻辑。 See if this solve your problem. 看看这是否能解决您的问题。

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")
}

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

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