首先,您应该从不希望在表视图中显示的数组中筛选出元素。然后,您将对数据源使用筛选后的数组。你不必写在你的
cellForRowAt:..
功能。
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let arrayOne = [One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "2"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "2"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1")]
var filteredArray = [One]()
@IBOutlet weak var compareTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
filteredArray = arrayOne.filter { $0.cellID == $0.cellID2 }
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CompareTableViewCell") as! CompareTableViewCell
let arrayID = filteredArray[indexPath.row]
return cell
}
}