简体   繁体   English

UIView子视图不响应更改

[英]UIView subview not responding to changes

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. 值得注意的是,我正在UITableCell中搜索子视图,UITableView大约有5行。

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

After putting the %p in the NSLog you can the memory address address is the same. 将%p放入NSLog后,您可以使内存地址地址相同。 Other tag=6 lines have different addresses so I should expect at least that cell to move. 其他tag = 6行具有不同的地址,因此我至少应该期望该单元格移动。

I prefer to subclass UITableViewCell, then I can access what I want by properties. 我更喜欢对UITableViewCell进行子类化,然后可以通过属性访问所需的内容。 I don't like -viewWithTag: , it gave me problems before and made codes hard to manage. 我不喜欢-viewWithTag:,它以前给我带来了问题,并使代码难以管理。

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. 在这种情况下,静态类型(UIView *与UILabel *)不会更改编译器生成的代码。

The third example should NSLog each view it operates on. 第三个示例应使用NSLog对其进行操作的每个视图。 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). 检查if (subview2.tag == 6)类的东西也很有意义,以查看是否有多个具有相同标签的视图(听起来像有)。

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. 您的日志消息还可以打印视图描述(或仅使用“%p”格式显示视图地址),以查看您使用的视图是否相同。

You need to run your viewWithTag statement on each cell, not on the entire tableView. 您需要在每个单元格而不是整个tableView上运行viewWithTag语句。 This should most likely be set in cellForRowAtIndexPath, and then you would reload the rows that changed when you need to. 这很可能应该在cellForRowAtIndexPath中设置,然后您将重新加载需要更改的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM