在UITableViewController
類別中負責填充單元格的TableView
,我想過濾一些單元格,但我無法弄清楚如何做到這一點。TableView如何過濾表格中的某些單元格
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return meals.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "MealTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! MealTableViewCell
let meal = meals[indexPath.row]
cell.title.text = meal.name
return cell
}
我有屬性meal.veg : Bool
和理想,我想只與meal.veg == true
飯菜來填充TableView
。我堅持的是,我無法真正瞭解override func tableView()
如何填充表格。我的意思是如何調用這個函數?我怎樣才能過濾細胞。
因爲函數的返回類型是UITableViewCell
而且不是可選的,所以我必須返回一個單元格,這不允許單元格過濾的機會。
您需要對陣列數據源執行過濾器:「飯食」。就像'meals.filter({$ 0.veg == true})'' – Larme