0

I have an UITableView which holds a list of items. For each UITableViewcell in that table, depending on a property from the associated object to that cell, I toggle the visibility of an UIButton by changing the height constraint constant to 0, or to a value defined to make it visible. I've checked the Clip to Bounds option on the Xcode designer for that button.

If I feed the table view a list of items that set some of the buttons visible, and others hidden and scroll, the cells that had the button visible may have it hidden, and vice-versa. This is more noticeable when there's few cells with the button, and the rest without it.

The method that contains the logic to show or hide the UIButton is from within the UITableViewCell custom class for the cells, as it follows:

public partial class UITableViewCellCustom : UITableViewCell
{
    public Object obj;

    public void SetObject(Object obj)
    {
        // Do something with obj...

        // Do something with the obj that determines if the buttons should be collapsed or not
        Boolean collapseButton = ...;

        ToggleButtonVisibility(collapseButton);
    }

    private void ToggleButtonVisibility(Boolean collapse) 
    {
        NSLayoutConstraint uiButtonCancelHeightConstraint = UIButtonCancel.Constraints
            .FirstOrDefault(query => query.FirstItem == UIButtonCancel
                                  && query.FirstAttribute == NSLayoutAttribute.Height);
                                  
        NSLayoutConstraint uiButtonCancelTopConstraint = this.ContentView.Constraints
            .FirstOrDefault(query => query.FirstItem == UIButtonCancel
                                  && query.FirstAttribute == NSLayoutAttribute.Top);

        if (collapse)
        {
            uiButtonCancelHeightConstraint.Constant = 0;
            uiButtonCancelTopConstraint.Constant = 0;
        }
        else
        {
            uiButtonCancelHeightConstraint.Constant = 30;
            uiButtonCancelTopConstraint.Constant = 10;
        }
    }
}

The SetObject method is called from the UITableViewSource class that gets the object from the correct index and sets it to the cell ( No problem here ). Then, while some UILabels texts are changed with the values from the object, I check if the button is required or not ( No problem here ). When I call the ToggleButtonVisibility method, and attempt to change the two constraints -- height and top -- the values are applied, the top constraint is visibly changed, but the height constraint seems to be ignored when the cell is reused.

I've tried to force the ClipToBounds to true, force the method in the main thread, but none of them worked. What am I missing here?


Forgot to mention: When the button is pressed, the table view is cleared ( I feed the source an empty list, and reload the data ), an long task is performed, and then a new list is applied to the table, but the cell in question remains with the button bugged.


Notes:

  • Hiding the button by changing the Alpha to 0 or by set the Hidden to true is not an option, since it will leave an hole within the tableview.
Community
  • 1
  • 1
auhmaan
  • 726
  • 1
  • 8
  • 28
  • Do you need to change the constraints to toggle visibility? Can't you just change Alpha or Hidden flag of the button? – lowleetak Dec 06 '17 at 22:41
  • I agree with @lowleetak why don't you set `alpha = 0` or `hidden = true` to hide the button? – ColeX Dec 07 '17 at 05:07
  • @lowleetak Unfortunately, if I toggle the visibility by changing the Alpha / Hidden flag, the list will contain _holes_ in it, so it's not an option. – auhmaan Dec 07 '17 at 16:38
  • Also, you have tried to update the data for UITableView and call tableView.ReloadData or tableView.RemoveRow? Modifying the constraint will still leave the cell in the TableView and you might have TableView separator still showing. – lowleetak Dec 07 '17 at 21:54
  • I've tried to feed an empty array to the table view, reloading the data, and then feed the "live" data and reloading again to ensure all the data and rows are updated. It's still happens. – auhmaan Dec 11 '17 at 09:57

1 Answers1

0

Fixed this problem by wrapping the UIButton on a UIView and then resizing the view depending on my needs.

auhmaan
  • 726
  • 1
  • 8
  • 28