[英]How can I get UILabel on a custom table view cell?
In my custom cell there is a UILabel
and one UIButton
. 在我的自定义单元中,有一个UILabel
和一个UIButton
。 Whenever I click on that UIButton
the UILabel
's height is increased as per content of the label. 每当我单击该UIButton
, UILabel
的高度都会根据标签的内容而增加。
The problem is that how can I get that UILabel
that is a subview of a cell. 问题在于如何获取作为单元格子视图的UILabel
。 Every cell contains a label and a button but only that UILabel
is increased that the cell button is clicked, others remain as they are. 每个单元格都包含一个标签和一个按钮,但是单击该单元格按钮只会增加UILabel
,其他单元格则保持原样。
Thanks in advance. 提前致谢。
为此,您需要为每个组件分配TAG。
Try bind the method to your button may work for you. 尝试将方法绑定到您的按钮可能适合您。
-(void)buttonClick:(id)sender {
UITableViewCell *cell=(UITableViewCell*)[sender superview];
NSArray *subviews=[cell subviews];
for (int i=0; i<[subviews count]; i++) {
id object=[subviews objectAtIndex:i];
if([object isKindOfClass:[UILabel class]]){
//do stuff here
}
}
}
The easiest way is to assign the tag
property to the labels. 最简单的方法是将tag
属性分配给标签。 That way in your button action method you can find the relevant label. 这样,您可以在按钮操作方法中找到相关标签。 For example: 例如:
- (void) myButtonAction: (UIButton *) sender
{
UILabel *theLabel = (UILabel *) [sender.superview viewWithTag: MYLABELTAG];
// Do something with theLabel
}
What this code does is finds the label in the same view as the button is in. 该代码的作用是在与按钮所在的视图相同的视图中找到标签。
You can go through all subview
s in the cell and see which one is a UILabel
: 您可以遍历单元格中的所有subview
,看看哪个是UILabel
:
for(UIView *v in [cell subviews])
{
if([v isKindOfClass:[UILabel class]])
//you can put more checks to see if height is to be increased/decreased
[v setFrame:CGRectMake(origin.x,origin.y,width,height)];
}
I wrote a short example on how to get labels from tablecells here: https://stackoverflow.com/a/9864169/407488 我在这里写了一个简短的示例,说明如何从表格单元中获取标签: https ://stackoverflow.com/a/9864169/407488
But i think what you want is to increase all labels in all cells at once? 但是我想您想要的是一次增加所有单元格中的所有标签? Then just use reloadData
and change a member variable before calling it (when touching the button). 然后只需使用reloadData
并在调用它之前更改成员变量(触摸按钮时)。 You read that member in your cellForRow:
code and assign the height appropriate. 您在cellForRow:
代码中读取了该成员,并指定了适当的高度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.