[英]How to get cell data from button pressed event (Custom tableview cell)
There is a button and label on my custom tableview cell. 我的自定义表格视图单元格上有一个按钮和标签。 This is how Im set the button action. 这就是我设置按钮动作的方式。
[cell.btnPlus addTarget: self action: @selector(plusButtonPressed) forControlEvents: UIControlEventTouchUpInside];
This method is working fine. 这种方法很好用。 I want to get the label text of the cell which the button is pressed. 我想获取按钮被按下的单元格的标签文本。
How could I do it? 我该怎么办?
将属性添加到指向单元格的按钮,或者,如果btnPlus是单元格的子视图,请使用btnPlus.superview.textlabel.text
。
将标签设置为该按钮并进行标签,然后可以使用带有标签选项的视图获取标签文本
change @selector(plusButtonPressed) to @selector(plusButtonPressed:) (note the added colon at the end) 将@selector(plusButtonPressed)更改为@selector(plusButtonPressed:)(注意最后添加的冒号)
then in your 然后在你的
- (void)plusButtonPressed:(UIButton*)sender {
NSString *theText = sender.superview.textlabel.text;
}
(this is assuming the plus button is added to the contentView of the UITableViewCell) (这是假定加号按钮已添加到UITableViewCell的contentView中)
set tag to cell: 将标签设置为单元格:
cell.contentView.tag = indexPath.row+10000*indexPath.section; //or any you like
[cell.contentView addSubview:button];
then, in button's callback: 然后,在按钮的回调中:
int raw=0;
int section=0;
long tag = [sender superview].tag;
NSLog(@"tag: %ld", tag);
for (int i = 0; i<[data count]; i++) {
if (tag>=10000)
{
tag-=10000;
section++;
}
else i = [data count];
}
raw = tag;
now, you have full access to your cell. 现在,您可以完全访问您的单元了。 use calculated raw
and section
ta get access to cell. 使用计算出的raw
和section
ta可以访问单元格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.