Hide Static cell on TableViewController

hi guys

I am working on my first Xamarin IOS app and I'm having a little bit of trouble trying to hide a static cell in a TableViewController. Actually, I can make the cell to be hidden successfully but because this cell is not the first one in the section the other cells below that one don't move up to occupy that empty space

Please see attached picture to get a better idea of what I am taking here.

This is the code where I hide one cell or another depending on a switch value. How can I make the whole cell disappear completely and not keeping that empty space there?

partial void ImportarNumer_ValueChanged(UISwitch sender)
{
    if (this.ImportarNumeroSwitch.On)
    {

        this.MultipleNumberCell.Hidden = false;
        this.SingleNumberCell.Hidden = true;
    }
    else {

        this.MultipleNumberCell.Hidden = true;
        this.SingleNumberCell.Hidden = false;
    }
}

Best Answers

  • ErnestoRuanoErnestoRuano US
    Accepted Answer

    I was able to make it works... The GetHeightForRow method is inly being fired at the beginning and also when you call viewtable.ReloadData() so in the switch value change event i just call the reloadData method and change the rowToHide value:

    partial void ImportarNumer_ValueChanged(UISwitch sender)
    {
        if (this.ImportarNumeroSwitch.On)
        {
            this.sectionToHide = 1;
            this.rowToHide = 1;
        }
        else
        {
            this.sectionToHide = 1;
            this.rowToHide = 0;
        }
    
        this.RecargaTableView.ReloadData();
    }
    
    public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
    {
        if (indexPath.Section == this.sectionToHide && indexPath.Row == this.rowToHide)
        {
            return 0;
        }
    
        return base.GetHeightForRow(tableView, indexPath);
    }
    

    Thank for all the help guys

Answers

  • TedRogersTedRogers USMember ✭✭✭✭

    @ErnestoRuano I rarely use static cells so I'm not sure how to handle that but I know you can deal with it using "normal" cells.

  • sonny_ppgsonny_ppg USMember, University ✭✭

    Override GetHeightForRow in the table's view controller and set the height of the row to 0.

    public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
    {
    if(indexPath.Row == rowIndex)
    {
    return 0;
    }
    else
    return base.GetHeightForRow(tableView, indexPath);
    }

  • That's a good idea but still having a problem. I have to change the row's height based on the switch value and the GetHeightForRow is only executed when the page is opening. After the user change the value of the switch, it is not being fired anymore

  • @TedRogers said:
    @ErnestoRuano I rarely use static cells so I'm not sure how to handle that but I know you can deal with it using "normal" cells.

    @TedRogers I am using static cells since I have a static form. Is there any way to create a static form using dynamic cells ?

  • ErnestoRuanoErnestoRuano USMember
    Accepted Answer

    I was able to make it works... The GetHeightForRow method is inly being fired at the beginning and also when you call viewtable.ReloadData() so in the switch value change event i just call the reloadData method and change the rowToHide value:

    partial void ImportarNumer_ValueChanged(UISwitch sender)
    {
        if (this.ImportarNumeroSwitch.On)
        {
            this.sectionToHide = 1;
            this.rowToHide = 1;
        }
        else
        {
            this.sectionToHide = 1;
            this.rowToHide = 0;
        }
    
        this.RecargaTableView.ReloadData();
    }
    
    public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
    {
        if (indexPath.Section == this.sectionToHide && indexPath.Row == this.rowToHide)
        {
            return 0;
        }
    
        return base.GetHeightForRow(tableView, indexPath);
    }
    

    Thank for all the help guys

Sign In or Register to comment.