简体   繁体   English

如何在自定义表格视图单元格上获取UILabel?

[英]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. 每当我单击该UIButtonUILabel的高度都会根据标签的内容而增加。

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.

相关问题 iPhone-如何从自定义表格单元格(动态)引用UILabel? - iphone - How can I referencing a UILabel from a custom table cell (dynamic)? 我已经通过自定义单元格将textfield插入到表格视图中,现在如何从textview获取数据 - i have inserted textfield into table view through custom cell now how to get the data from the textview 当我还使用自定义表格视图单元格时,如何通过NSArray设置表格视图的文本? - How can i set The text of my Table View, via an NSArray, when im also using a Custom table View Cell? iphone自定义表格单元格,UILabel需要可滚动 - iphone Custom table cell, UILabel needs to be scrollable iPhone-自定义单元-无法设置UILabel - Iphone - Custom cell - can't set UILabel 无法分配给searchResultsTableView的自定义单元格的UILabel - Can't assign to the UILabel of custom cell of searchResultsTableView 如何在表格视图单元格上设置突出显示的图像? - How can I set a highlighted image on a table view cell? 如何为我的表格单元创建一个附件视图? - How can I create an accessory view for my table cell like this? 如何将自定义图像放在表格视图单元格中 - how to put custom image in table view cell 如何在表格视图单元格上创建自定义按钮 - how to create custom button on table view cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM