【发布时间】:2011-03-10 19:23:25
【问题描述】:
我正在学习如何处理子视图,但我很难操纵其中一个的位置。每个子视图都有一个唯一的标签。值得注意的是,我在 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 标签=6 0x4b59610
2011-03-10 19:53:42.344 标签=842 标签=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 标签=6 0x4b59610
将 %p 放入 NSLog 后,内存地址地址就可以相同了。其他 tag=6 行具有不同的地址,因此我应该期望至少该单元格会移动。
【问题讨论】:
-
self.view是tableView吗?标签是否在所有表格单元格中都是唯一的?你在哪里为你的标签设置标签?
-
是的。每个标签对于单元格都是唯一的,但不是整个表格,因为表格单元格是重复的。我在 IB 中设置标签。
-
啊...那么您只需要在 1 个单元格上执行 viewWithTag,而不是在整个 tableView 上。如果你在 tableView 上执行 viewWithTag,你只会得到它遇到的那个标签的第一个实例。
标签: iphone objective-c ios uiview subview