如何过滤我的 table 视图单元格,以便过滤掉的单元格不显示?
How do I filter my table view cells so that the cells filtered out do not appear?
我正在尝试根据用户在 UIPickerView
中所做的选择来更改显示的单元格。现在,我正在使用 cell.hidden = true
方法。然而,这个问题是单元格隐藏但是有白色空白 space 隐藏单元格的大小。我想使 table 视图中的单元格为 hidden/removed(已保存数据)。这是当前代码。
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 和 return 0 用于您不想显示的行
另一种方法是从 tableData 中完全删除这些行。所以也许有一个 "filteredArray" 和一个未过滤的数组。这样你总是可以 return 在不过滤时返回到你的非过滤数组
不要隐藏单元格,更改您的数据。
但我不得不说您当前的 cellForRowAtIndexPath
是一团糟,因此我目前无法为您提供解决您具体情况的代码。我只能给你一般的建议。
建议不要在您的 cellForRowAtIndexPath
中包含太多逻辑。隐藏你的细胞听起来像是一个非常非常非常糟糕的主意。当你想隐藏一个单元格时,从支持数据结构和reload
tableview
中删除它的数据。每当我处理某种可过滤的 table- 或集合视图时,我都有一个数组来保存所有可以显示的数据。但实际上无处不在的是它的 filtered copy
。这样可以很容易地删除您应用的任何过滤器,并且您可以完全灵活地删除要隐藏的任何单元格。
我在您的代码中发现了几个问题:
tableData[indexPath.row + 25]
- 为什么 +25
?
- 5个不同的数据数组!使用 class 来保存为分布在 5 个数组中的每个数据条目存储的值
lblOneBool
、lblTwoBool
- 那些是什么?不要只是枚举你的变量,如果它们只是 4 个具有相似功能的布尔值,请使用数组代替或更好的命名。
- 如前所述:您的
cellForRowAtIndexPath
中的业务逻辑过多
我正在尝试根据用户在 UIPickerView
中所做的选择来更改显示的单元格。现在,我正在使用 cell.hidden = true
方法。然而,这个问题是单元格隐藏但是有白色空白 space 隐藏单元格的大小。我想使 table 视图中的单元格为 hidden/removed(已保存数据)。这是当前代码。
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 和 return 0 用于您不想显示的行
另一种方法是从 tableData 中完全删除这些行。所以也许有一个 "filteredArray" 和一个未过滤的数组。这样你总是可以 return 在不过滤时返回到你的非过滤数组
不要隐藏单元格,更改您的数据。
但我不得不说您当前的 cellForRowAtIndexPath
是一团糟,因此我目前无法为您提供解决您具体情况的代码。我只能给你一般的建议。
建议不要在您的 cellForRowAtIndexPath
中包含太多逻辑。隐藏你的细胞听起来像是一个非常非常非常糟糕的主意。当你想隐藏一个单元格时,从支持数据结构和reload
tableview
中删除它的数据。每当我处理某种可过滤的 table- 或集合视图时,我都有一个数组来保存所有可以显示的数据。但实际上无处不在的是它的 filtered copy
。这样可以很容易地删除您应用的任何过滤器,并且您可以完全灵活地删除要隐藏的任何单元格。
我在您的代码中发现了几个问题:
tableData[indexPath.row + 25]
- 为什么+25
?- 5个不同的数据数组!使用 class 来保存为分布在 5 个数组中的每个数据条目存储的值
lblOneBool
、lblTwoBool
- 那些是什么?不要只是枚举你的变量,如果它们只是 4 个具有相似功能的布尔值,请使用数组代替或更好的命名。- 如前所述:您的
cellForRowAtIndexPath
中的业务逻辑过多