开发者

Monotouch: Remove all subviews from a view

开发者 https://www.devze.com 2023-02-08 01:30 出处:网络
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++)

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++)
        {
    开发者_如何学JAVA        this.Subviews[i].RemoveFromSuperview ();

        }


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.


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.


Try forcing a refresh of the view afterward, or invoking the Remove call specifically on the main thread.


If you absolutely need to use an for loop, this will do

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号