Monotouch: delete all views in the view

I am trying to remove all subviews from a UIView. I tried the following:

for (int i = 0; i < this.Subviews.Length; i++) { this.Subviews[i].RemoveFromSuperview (); } 
+7
source share
4 answers

Just checked it and it worked for me. (Although your code also looks good to me ...)

 foreach (UIView view in tableView.Subviews) { view.RemoveFromSuperview(); } 

If this does not work for you, there may be something that prevents the removal of the subview.

+12
source

The problem with your sample is how you built the loop.

When you delete a view at 0, the Subviews array is one shorter, and element 1 becomes element 0 at the next iteration. On the other hand, your self variable continues to grow, so you skip view 1.

+4
source

Try to force the view to refresh after this, or call the Delete call directly in the main thread.

0
source

If you absolutely must use a for loop, this will do

  for (int i = this.Subviews.Length - 1 ; i > 0 i--) { this.Subviews[i].RemoveFromSuperview (); } 
0
source

All Articles