ios - 如何过滤 TableView 单元格以便过滤掉的单元格不会出现?

标签 ios swift uitableview uipickerview

我正在尝试根据用户在 UIPickerView 中所做的选择来更改显示的单元格。现在,我正在使用方法cell.hidden = true。然而,这样做的问题是单元格隐藏了,但存在隐藏单元格大小的空白区域。我想这样做,以便从 TableView 中隐藏/删除单元格(保存数据)。这是当前代码。

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 委托(delegate)的 heightForRowAtIndexPath 并为不想显示的行返回 0

另一种方法是从 tableData 中完全删除这些行。所以也许有一个“filteredArray”和一个非过滤数组。这样,您可以在不过滤时始终返回到未过滤的数组

关于ios - 如何过滤 TableView 单元格以便过滤掉的单元格不会出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34440479/

相关文章:

ios - Swift 线程安全计数器变量?用于跟踪何时删除网络事件指示器

iOS - Xcode 线程 1 EXC_BAD_INSTRUCTION

ios - 如何在异步图像下载和调整图像高度后更新 tableview 单元格

html - 固定图像在 HTML 字符串中的位置

ios - 我是否需要完全废弃我的应用程序才能添加核心数据?

swift - 选择新的 UITextField 时更新 UIPickerView 行

ios - 在标题部分做与单元格相同的动画

IOS swift 如何用返回的 Json 数据填充我的 TableView

ios 二进制 .app 文件不可点击

ios - swift API 请求和 JSON 解析