iPhone中的removeFromSuperView有什么用?

发布于 2024-09-26 15:56:40 字数 238 浏览 3 评论 0原文

在我的程序中,如果我将以下行放入 cellForRowAtIndexPath(Tableview) 中,滚动就可以了。否则,线路就会崩溃。我想知道这段代码做了什么?

代码是......

for (UIView *oldViews in cell.contentView.subviews)
{
    [oldViews removeFromSuperview];
}

提前致谢

In my program, if i put the below line in cellForRowAtIndexPath(Tableview), the scrolling is fine. Otherwise, the lines are crashed. I want to know what this code did?

The code is....

for (UIView *oldViews in cell.contentView.subviews)
{
    [oldViews removeFromSuperview];
}

Thanks in advance

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

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

发布评论

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

评论(4

酒绊 2024-10-03 15:56:40

你必须知道在 iOS 中你正在操纵“视图”。视图是 UI 部分(图像、标签、输入等)或包含层。

在启动开始时,您必须向窗口添加一个视图。然后您可以在视图中添加任意数量的视图。

如果在视图A上添加视图B。并且在窗口上添加视图A。

语义是:

  • 视图 A 是 B 的超级视图

  • 视图 B 是 A 的子视图

  • 视图 A 是窗口的子视图

  • 窗口是视图A的超级视图

    因此,如果您在 B 上调用removeFromSuperview,则会将 B 删除到 A 上(并显示)。

请注意:

当您添加子视图 (addSubview:) 时,将对添加的视图执行保留。

当您删除视图(removeFromSuperview:或removeSubviewAtIndex:)时,将对删除的视图执行释放。

回答您最初的问题

for (UIView *oldViews in cell.contentView.subviews)
{
    [oldViews removeFromSuperview];
}

对每个 cell.contentView 子视图执行removeFromSuperview 方法。
因此旧视图会从屏幕上删除,但不需要释放(它们被释放,因此retainCount - 1)。

You have to know that in iOS you are manipulating "views". Views are UI parts (images, labels, inputs etc.) or a containing layer.

At the launch beginning you MUST add a view to you window. Then you can add add as many views your want on your view.

If you add a view B on a view A. And the view A on the window.

Semantic is :

  • View A is the superview of B

  • View B is a subview of A

  • View A is a subview of the window

  • The window is the superview of view A

    So if you invoke removeFromSuperview on B, you removing B to be on A (and to be displaying).

Please note :

When you add a subview (addSubview:) a retain is performed on the view added.

When you remove a view (removeFromSuperview: or removeSubviewAtIndex:) a release is performed on the view removed.

To answer to you initial question

for (UIView *oldViews in cell.contentView.subviews)
{
    [oldViews removeFromSuperview];
}

Perform the removeFromSuperview method on every cell.contentView subviews.
So old views are removed from the screen but not necessary deallocated (they are released so retainCount - 1).

饮惑 2024-10-03 15:56:40

然后很简单

“cell.contentView.subviews”中的每个UIView(因此cell.contentView的子视图)都被删除。

它会将它们从超级视图中删除,并且如果保留计数为0,则可能会调用dealloc方法。

Then it's simple

Each UIView inside "cell.contentView.subviews" (so the subviews of cell.contentView), are removed.

It will remove them from the superview, and probably call the dealloc method if the retainCount is 0.

流云如水 2024-10-03 15:56:40

它只是从 cell.contentView 中删除所有子视图。

It simply removes all subviews from cell.contentView.

鱼窥荷 2024-10-03 15:56:40

每个 UIView 类或子类都可以通过 addSubView: 维护自己的视图层次结构,这些代码将从 cell.contentView 中删除所有子视图。

但是 UITableViewCell 的实例经常被 出队ReusableWithIdentifier:。这些代码将删除其所有子视图,因此如果需要重用它,则必须再次重新构建其所有子视图。

Each UIView class or sub-class can maintain his view hierarchies by addSubView:, and those codes will remove all subviews from cell.contentView.

But the instances of UITableViewCell often re-used by dequeueReusableWithIdentifier:. Those codes will remove all its subviews so if it needs to be reused, it must re-build its all sub views again.

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