从 UIScrollView 中删除 UIView 时出现问题

发布于 2024-11-10 15:29:43 字数 1316 浏览 0 评论 0原文

我正在尝试编写代码,将玩家添加到基于对象的视图中,但是我遇到了一些问题。

目前,如果我在playerList中使用4个已确认工作的Player对象运行initView方法,则只有3个UIView会出现在我的滚动视图中,然后当我单击清除玩家按钮时,只有最后一个(第3个)UIView将被删除。

附注,我的自定义按钮也没有出现在 UIView 中,我有一个它们应该加载的图像,但它不起作用。

预先感谢您的任何帮助。

- (void)clearPlayers {
    for (Player* i in self.playerList) {
        [i.viewPane removeFromSuperview];
    }
    [self.playerList removeAllObjects];
}

- (void)initView {
    int Loc = 0;
    int Count = 1;
    int margin = 5;
    int height = 100;
    for (Player *p in playerList) {
        UIView *playerView = [[UIView alloc] initWithFrame:CGRectMake(0, Loc, 320.0, height)];
        p.viewPane = playerView;
        [playerView setBackgroundColor:[UIColor redColor]];
        [scrollView addSubview:playerView];
        UIButton *plus = [[UIButton alloc] initWithFrame:CGRectMake(200, 10, (height - 5), (height - 5))];
        UIImage *buttonImage =[[UIImage alloc] initWithContentsOfFile:@"Metal_Plus_Up_2.png"];
        [plus setBackgroundImage:buttonImage forState:UIControlStateNormal];
        [p.viewPane addSubview:plus];

        [plus release];
        [playerView release];
        Loc = Loc + (height + margin);
        Count = Count + 1;
    }
    [scrollView setContentSize:CGSizeMake(320.0, (height * Count) + (margin * Count))];
}

I'm trying to make code that will add players to a view based on objects but, I'm having some issues.

Right now currently if I run the initView method with 4 confirmed working Player objects in playerList, only 3 UIViews will appear in my scrollview, then when I click the clear players button, only the last (3rd) UIView will be removed.

A side note, none of my custom buttons appear in the UIViews either, I have an image that they should load with, but its not working.

Thanks in advance for any help.

- (void)clearPlayers {
    for (Player* i in self.playerList) {
        [i.viewPane removeFromSuperview];
    }
    [self.playerList removeAllObjects];
}

- (void)initView {
    int Loc = 0;
    int Count = 1;
    int margin = 5;
    int height = 100;
    for (Player *p in playerList) {
        UIView *playerView = [[UIView alloc] initWithFrame:CGRectMake(0, Loc, 320.0, height)];
        p.viewPane = playerView;
        [playerView setBackgroundColor:[UIColor redColor]];
        [scrollView addSubview:playerView];
        UIButton *plus = [[UIButton alloc] initWithFrame:CGRectMake(200, 10, (height - 5), (height - 5))];
        UIImage *buttonImage =[[UIImage alloc] initWithContentsOfFile:@"Metal_Plus_Up_2.png"];
        [plus setBackgroundImage:buttonImage forState:UIControlStateNormal];
        [p.viewPane addSubview:plus];

        [plus release];
        [playerView release];
        Loc = Loc + (height + margin);
        Count = Count + 1;
    }
    [scrollView setContentSize:CGSizeMake(320.0, (height * Count) + (margin * Count))];
}

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

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

发布评论

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

评论(1

归途 2024-11-17 15:29:43

问题:您是否已验证从 initWithContentsOfFile 返回的 UIImage 不为零?您可能需要完整路径而不仅仅是文件名

就 UIViews 未删除的怪异而言,据我所知,您发布的所有内容看起来都很好。我唯一能想到的是,也许您没有将 retain 指定为 viewPane 属性的属性...

Question: have you verified that the UIImage returned from initWithContentsOfFile is not nil? You might need the full path instead of just the filename

As far as the wackiness with the UIViews not getting removed goes, everything you've posted looks fine as far as I can see. The only thing I can think of is that maybe you don't have retain specified as an attribute for your viewPane property...

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