iPhone:如何从 UIScrollView 中删除所有对象

发布于 2024-09-09 16:51:40 字数 87 浏览 5 评论 0原文

基本上我想从 UIScrollView 中删除所有对象,但我还没有找到解决方案,因为简单的“removeAllObjects”命令不起作用。有人知道该怎么做吗?

Basically I want to remove all objects from a UIScrollView and I haven't yet found a solution to it because a simple "removeAllObjects" command doesn't work. Does anyone have an idea how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

定格我的天空 2024-09-16 16:51:40

更简单:

[[scrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

Even easier:

[[scrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
洋洋洒洒 2024-09-16 16:51:40

最简单的方法:

for(UIView *subview in [scrollView subviews]) {

    [subview removeFromSuperview];

}

The easiest way:

for(UIView *subview in [scrollView subviews]) {

    [subview removeFromSuperview];

}
清风疏影 2024-09-16 16:51:40

在不删除滚动条的情况下,有两种解决方案:

向所有子视图添加标签,然后使用这些标签删除它们。

实现:

将其放在类上方

#define XPScrollViewSubviewStartTag             100

当您添加对象时

for(NSInteger = 0;i<[viewArray count];i++){
     UIView* view = [viewArray objectAtIndex:i];
     view.tag = XPScrollViewSubviewStartTag + i;
     [scrollView addSubview:view];
}

当您稍后想要删除视图时 使用

for(UIView* view in [scrollView subviews]){
      if(view.tag >= XPScrollViewSubviewStartTag)
           [view removeFromSuperview];
}

创建一个自定义 UIView 类

当您想要删除视图时

for(id view in [scrollView subviews]){
      if(![view isKindOfClass:[CustomView class]])
            continue;

      //not necessary, but just to make things understandable
      CustomView* customView = (CustomView*)view;
      [customView removeFromSuperview];
}

Two solutions would be, without removing the scrollbars:

Add a tag to all of your subviews, remove them by using those tags.

Implementation:

Place this above your class

#define XPScrollViewSubviewStartTag             100

When you add the objects

for(NSInteger = 0;i<[viewArray count];i++){
     UIView* view = [viewArray objectAtIndex:i];
     view.tag = XPScrollViewSubviewStartTag + i;
     [scrollView addSubview:view];
}

When you later want to delete the views use

for(UIView* view in [scrollView subviews]){
      if(view.tag >= XPScrollViewSubviewStartTag)
           [view removeFromSuperview];
}

Create a custom UIView class

When you want to remove the views

for(id view in [scrollView subviews]){
      if(![view isKindOfClass:[CustomView class]])
            continue;

      //not necessary, but just to make things understandable
      CustomView* customView = (CustomView*)view;
      [customView removeFromSuperview];
}
秋风の叶未落 2024-09-16 16:51:40

Oritm 的解决方案很好,但是如果您不想使用标签,您可以手动删除要从滚动视图中删除的视图。这将假设您知道要删除哪个视图。例如,我的滚动视图只有 UILabels,因此当我运行以下命令时:

// This will remove the scrollview but the scrollbars will remain 
for (UIView *subview in [self.detailsExpander.scrollView subviews]) {
    if ([NSStringFromClass([subview class]) isEqualToString:@"UILabel"])
        [subview removeFromSuperview];
}

这只会删除所有标签,但保留滚动条(它们是图像视图)。注意:如果您想保留特定的图像视图,同时又想保留滚动条,则此方法将不起作用。

Oritm's solution is nice, but if you don't want to bother using tags, you can manually remove which views you want to remove from the scrollview. This will assume that you know which view to remove. For example, my scrollview only has UILabels so when I run the following:

// This will remove the scrollview but the scrollbars will remain 
for (UIView *subview in [self.detailsExpander.scrollView subviews]) {
    if ([NSStringFromClass([subview class]) isEqualToString:@"UILabel"])
        [subview removeFromSuperview];
}

This will only remove all the labels but keep the scrollbars (which are image views). Note: this won't work if you want to keep specific image views while at the same time you want to keep the scrollbars.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文