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 UILabel
s 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
to0
or by set theHidden
totrue
is not an option, since it will leave an hole within the tableview.