UIView 子视图不响应更改

发布于 2024-10-21 04:31:51 字数 2024 浏览 1 评论 0原文

我正在学习如何处理子视图,但我很难操纵其中之一的位置。每个子视图都有一个唯一的标签。值得注意的是,我正在 UITableCell 中搜索子视图,UITableView 大约有 5 行。

如果我这样做:

UIView *mike = [self.view viewWithTag:6];
mike.frame = CGRectMake(250, 5, 25, 20);
mike.backgroundColor = [UIColor redColor];
NSLog(@"mike=%@ tag=%d",[[mike class] description], [mike tag]);

或:

UILabel *label = (UILabel *)[self.view viewWithTag:6];
label.frame = CGRectMake(250, 5, 25, 20);
label.backgroundColor = [UIColor redColor];
NSLog(@"label=%@ tag=%d",[label text], [label tag]);

子视图不会改变位置,但是如果我使用下面的代码搜索它,它确实可以工作。

for (UIView *subview0 in [self.view subviews])
{
  for (UIView *subview1 in [subview0 subviews])
  {
    for (UIView *subview2 in [subview1 subviews])
    {
      if ([[[subview2 class] description] isEqualToString: @"UILabel"]) 
      {
        [subview2 setText:@"mike"];
        subview2.frame = CGRectMake(250, 5, 25, 20);
        subview2.backgroundColor = [UIColor redColor];
      }
    }
   }
 }

非常感谢任何帮助。

迈克

编辑:从执行控制台

2011-03-10 19:53:42.344 mike=UILabel tag=6 0x4b59610
2011-03-10 19:53:42.344 label=842 tag=6 0x4b59610
2011-03-10 19:53:42.345 0-subview=PerformAnalysisCustomCell 标签=0
2011-03-10 19:53:42.345 1-subview=UIGroupTableViewCellBackground 标签=0
2011-03-10 19:53:42.346 2-subview=UIView 标签=0 0x4d62910
2011-03-10 19:53:42.349 1-subview=UITableViewCellContentView 标签=0
2011-03-10 19:53:42.349 2-subview=UILabel 标签=0 0x4b51320
2011-03-10 19:53:42.350 2-subview=UILabel 标签=1 0x4b59290
2011-03-10 19:53:42.350 2-subview=UILabel 标签=2 0x4b59370
2011-03-10 19:53:42.358 2-subview=UILabel 标签=3 0x4b59410
2011-03-10 19:53:42.359 2-subview=UILabel 标签=4 0x4b594b0
2011-03-10 19:53:42.360 2-subview=UILabel 标签=5 0x4b59560
2011-03-10 19:53:42.360 2-subview=UILabel tag=6 0x4b59610

将 %p 放入 NSLog 后可以看到内存地址地址是相同的。其他 tag=6 行具有不同的地址,因此我应该期望至少该单元格会移动。

I am learning how to handle subviews and I am having difficultly manipulating the position of one of them. Each subview has a unique tag. It is worth noting that I am searching for subviews in a UITableCell, the UITableView has about 5 rows.

If I do either this:

UIView *mike = [self.view viewWithTag:6];
mike.frame = CGRectMake(250, 5, 25, 20);
mike.backgroundColor = [UIColor redColor];
NSLog(@"mike=%@ tag=%d",[[mike class] description], [mike tag]);

or:

UILabel *label = (UILabel *)[self.view viewWithTag:6];
label.frame = CGRectMake(250, 5, 25, 20);
label.backgroundColor = [UIColor redColor];
NSLog(@"label=%@ tag=%d",[label text], [label tag]);

the subview does not change position, however if I search for it using the code below it does work.

for (UIView *subview0 in [self.view subviews])
{
  for (UIView *subview1 in [subview0 subviews])
  {
    for (UIView *subview2 in [subview1 subviews])
    {
      if ([[[subview2 class] description] isEqualToString: @"UILabel"]) 
      {
        [subview2 setText:@"mike"];
        subview2.frame = CGRectMake(250, 5, 25, 20);
        subview2.backgroundColor = [UIColor redColor];
      }
    }
   }
 }

Any help greatly appreciated.

Mike

EDIT: from console on execution

2011-03-10 19:53:42.344 mike=UILabel tag=6 0x4b59610
2011-03-10 19:53:42.344 label=842 tag=6 0x4b59610
2011-03-10 19:53:42.345 0-subview=PerformAnalysisCustomCell tag=0
2011-03-10 19:53:42.345 1-subview=UIGroupTableViewCellBackground tag=0
2011-03-10 19:53:42.346 2-subview=UIView tag=0 0x4d62910
2011-03-10 19:53:42.349 1-subview=UITableViewCellContentView tag=0
2011-03-10 19:53:42.349 2-subview=UILabel tag=0 0x4b51320
2011-03-10 19:53:42.350 2-subview=UILabel tag=1 0x4b59290
2011-03-10 19:53:42.350 2-subview=UILabel tag=2 0x4b59370
2011-03-10 19:53:42.358 2-subview=UILabel tag=3 0x4b59410
2011-03-10 19:53:42.359 2-subview=UILabel tag=4 0x4b594b0
2011-03-10 19:53:42.360 2-subview=UILabel tag=5 0x4b59560
2011-03-10 19:53:42.360 2-subview=UILabel tag=6 0x4b59610

After putting the %p in the NSLog you can the memory address address is the same. Other tag=6 lines have different addresses so I should expect at least that cell to move.

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

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

发布评论

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

评论(3

御弟哥哥 2024-10-28 04:31:51

您需要在每个单元格上运行 viewWithTag 语句,而不是在整个 tableView 上。这很可能应该在 cellForRowAtIndexPath 中设置,然后您可以在需要时重新加载更改的行。

You need to run your viewWithTag statement on each cell, not on the entire tableView. This should most likely be set in cellForRowAtIndexPath, and then you would reload the rows that changed when you need to.

鱼忆七猫命九 2024-10-28 04:31:51

我更喜欢子类化 UITableViewCell,然后我可以通过属性访问我想要的内容。我不喜欢 -viewWithTag:,它之前给我带来了问题,并使代码难以管理。

I prefer to subclass UITableViewCell, then I can access what I want by properties. I don't like -viewWithTag:, it gave me problems before and made codes hard to manage.

一影成城 2024-10-28 04:31:51

你的前两个例子做了完全相同的事情。静态类型(UIView* 与 UILabel*)不会更改编译器在这种情况下生成的代码。

第三个示例应该对它所操作的每个视图进行 NSLog。可能标签没有设置。

检查类似 if (subview2.tag == 6) 的内容也是有意义的,看看是否有多个视图具有相同的标签(听起来好像有)。

您的日志消息还可以打印视图描述(或简单地使用“%p”格式的视图地址),以查看您正在使用的视图是否相同。

Your first two examples do the exact same thing. The static type (UIView* vs UILabel*) doesn't change the code that the compiler produces in this case.

The third example should NSLog each view it operates on. Maybe the tag isn't set.

It would also make sense to check with something like if (subview2.tag == 6), to see if there are multiple views with the same tag (it sounds like there are).

Your log messages could also print the view description (or simply the view's address with the "%p" format) to see if the views you're using are the same.

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