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

时间:2018-06-08 03:18:30

标签: ios swift firebase firebase-storage uisearchcontroller

我有来自Firebase的数据,当加载数据时,我要么根据自定义单元格中的某些逻辑隐藏或显示图像。当数据未被过滤但第二个I在搜索栏中键入或将范围栏更改为图像不正确的不同索引时,它可以正常工作。 例如:索引0不应该有图像和索引1应该。它首次加载时的显示方式。但是,当我搜索时,我知道先前的索引1(现在索引0)应该仍然具有它的图像,但它不是。但是,如果我点击进入细节控制器,它会将我带到正确的页面。这就像它加载所有准确的信息,但在原始索引0上的逻辑。我会喜欢一些帮助,因为我一直在寻找答案永远。提前谢谢!

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

1 个答案:

答案 0 :(得分:3)

如果你在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")
}