我试图根据用户在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”和一个未过滤的数组。这样,您始终可以在不进行过滤时返回未过滤的数组
关于ios - 如何过滤表格 View 单元格,以免出现被滤除的单元格?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34440479/