Search code examples
iosswiftuitableviewuipickerview

How do I filter my table view cells so that the cells filtered out do not appear?


I am attempting to change which cells are displayed based on the selection a user makes in a UIPickerView. Right now, I am using the method cell.hidden = true. However, the problem with this is that the cell hides but there is white blank space the size of the cells that are hidden. I want to make it so the cells are hidden/removed (with data saved) from the table view. Here is the current code.

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

Solution

  • There are many different approaches you can do

    A simple one is to implement heightForRowAtIndexPath of the UITableView delegate and return 0 for rows you don't want to show

    Another approach would be to remove those rows from your tableData altogether. So maybe have a "filteredArray" and a nonfiltered array. That way you can always return back to your non filtered array when not filtering