Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monotouch: Remove all subviews from a view

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

        for (int i = 0; i < this.Subviews.Length; i++)
        {
            this.Subviews[i].RemoveFromSuperview ();

        }
like image 423
Ian Vink Avatar asked Feb 03 '11 07:02

Ian Vink


2 Answers

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

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

If it doesn't work for you, there might be something that prevents the subviews from being removed.

like image 128
riha Avatar answered Dec 21 '22 09:12

riha


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

When you remove the view at 0, the Subviews array is one element shorter, and element 1 becomes element 0 on the next iteration. Your i variable on the other hand keeps growing, so you end up skipping view 1.

like image 39
miguel.de.icaza Avatar answered Dec 21 '22 11:12

miguel.de.icaza