[英]How do I filter my table view cells so that the cells filtered out do not appear?
我試圖根據用戶在UIPickerView
所做的選擇來更改顯示的單元格。 現在,我正在使用方法cell.hidden = true
。 但是,與此有關的問題是該單元格已隱藏,但是存在白色空白,其大小與隱藏的單元格一樣。 我要這樣做,以便從表格視圖中隱藏/刪除單元格(保存數據)。 這是當前代碼。
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var numRows = tableData.count
if(timeTextfield.text == timeData[1]) {
numRows = 25
}
else if(timeTextfield.text == timeData[2]) {
numRows = 30
}
return numRows
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TblCell
var lblOneBool = true
var lblTwoBool = true
var lblThreeBool = true
var lblFourBool = true
if(timeTextfield.text == timeData[2]) {
cell.hidden = false
cell.lblTime.text = tableData[indexPath.row + 25]
cell.lblOne.text = lblOneData[indexPath.row + 25]
cell.lblTwo.text = lblTwoData[indexPath.row + 25]
cell.lblThree.text = lblThreeData[indexPath.row + 25]
cell.lblFour.text = lblFourData[indexPath.row + 25]
}
else {
cell.lblTime.text = tableData[indexPath.row]
cell.lblOne.text = lblOneData[indexPath.row]
cell.lblTwo.text = lblTwoData[indexPath.row]
cell.lblThree.text = lblThreeData[indexPath.row]
cell.lblFour.text = lblFourData[indexPath.row]
}
if(cell.lblOne.text != "Available") {
lblOneBool = false
}
if(cell.lblTwo.text != "Available") {
lblTwoBool = false
}
if(cell.lblThree.text != "Available") {
lblThreeBool = false
}
if(cell.lblFour.text != "Available") {
lblFourBool = false
}
if(playerTextfield.text == playersData[1]) {
if(lblOneBool || lblTwoBool || lblThreeBool || lblFourBool) {
cell.hidden = false
}
else {
cell.hidden = true
}
}
else if(playerTextfield.text == playersData[2]) {
if((lblOneBool && lblTwoBool) || (lblOneBool && lblThreeBool) || (lblOneBool && lblFourBool) || (lblTwoBool && lblThreeBool) || (lblTwoBool && lblFourBool) || (lblThreeBool && lblFourBool)) {
cell.hidden = false
}
else {
cell.hidden = true
}
}
else if(playerTextfield.text == playersData[3]) {
if((lblOneBool && lblTwoBool && lblThreeBool) || (lblOneBool && lblTwoBool && lblFourBool) || (lblOneBool && lblThreeBool && lblFourBool) || (lblTwoBool && lblThreeBool && lblFourBool)) {
cell.hidden = false
}
else {
cell.hidden = true
}
}
else if(playerTextfield.text == playersData[4]) {
if(lblOneBool && lblTwoBool && lblThreeBool && lblFourBool) {
cell.hidden = false
}
else {
cell.hidden = true
}
}
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 120
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let row = indexPath.row
performSegueWithIdentifier(conSegueIdentifier, sender: indexPath)
print(tableData[row])
if(timeTextfield.text == timeData[2]) {
tblIndex = row + 25
}
else {
tblIndex = row
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
//end of tableview
您可以執行許多不同的方法
一種簡單的方法是實現UITableView委托的heightForRowAtIndexPath,並為不想顯示的行返回0
另一種方法是從tableData中完全刪除這些行。 因此,也許有一個“ filteredArray”和一個未過濾的數組。 這樣,您始終可以在不過濾時返回到未過濾的數組
不要隱藏單元格,更改數據。
但是我不得不說,您當前的cellForRowAtIndexPath
是一團糟,因此,我目前無法為您提供解決您實際情況的代碼。 我只能給您一般建議。
該建議是不要在cellForRowAtIndexPath
包含太多邏輯。 隱藏您的單元格聽起來是一個非常非常糟糕的主意。 當您要隱藏單元格時,請從支持數據結構中刪除其數據,然后reload
tableview
。 每當我處理某種可過濾的表視圖或集合視圖時,我都會擁有一個數組,其中包含可以顯示的所有數據。 但實際上到處使用的是它的filtered copy
。 這樣就很容易刪除您應用的任何過濾器,並且您可以完全靈活地刪除要隱藏的任何單元格。
我在您的代碼中看到了幾個問題:
tableData[indexPath.row + 25]
-為什么+25
? lblOneBool
, lblTwoBool
那些是什么? 不要只枚舉您的變量,如果它們只是具有相似功能的4個布爾變量,請使用數組代替或更佳的命名。 cellForRowAtIndexPath
有太多業務邏輯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.