简体   繁体   中英

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.

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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