Я настроил свой tableView так, чтобы он фильтровал по значению 2 факторов каждого дочернего элемента массива (factor1 и factor2), сопоставляя его с 2 пользовательскими входами (factor1UserInput и factor2UserInput). Проблема в том, что мой numberOfSections настроен на возврат array.count - он загружает ВСЕ ячейки, включая незаполненные ячейки, для каждого дочернего элемента в массиве.
Итак, я пытаюсь установить для моего numberOfSections в моем tableView количество отфильтрованных ячеек (ячеек, которые я заполняю) - если нет лучшего способа отфильтровать их, чтобы не загружать незаполненные ячейки.
код NumberOfSections:
func numberOfSections(in tableView: UITableView) -> Int {
return array.count
}
код tableView:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ExampleCell", for: indexPath)
let preview = array[indexPath.section]
// BELOW LINE FILTERS THE DATA
if preview.factor1 == factor1UserInput && preview.factor2 == factor2UserInput {
// TRYING TO ONLY LOAD THESE CELLS
print("something matches the filter")
cell.imageView?.image = UIImage(named: "PlaceholderImage")
if let imageUrl = preview.imageUrl {
let url = URL(string: imageUrl)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async {
cell.imageView?.image = UIImage(data: data!)
}
}.resume()
}
} else {
}
return cell
}