简体   繁体   English

如何从按钮按下事件中获取单元格数据(自定义表格视图单元格)

[英]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. 使用计算出的rawsection ta可以访问单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从自定义tableview单元获取数据 - How to get the data from custom tableview cell 从自定义tableview单元格发送重新加载数据? - sending reload data from custom tableview cell? 无法访问tableview属性以获取自定义单元格中单击的按钮的索引 - Unable to access tableview property to get the index of button clicked in custom cell iPhone-如何确定自定义UITableViewCell中的哪个按钮被按下 - iPhone - How to determine in which cell a button was pressed in a custom UITableViewCell 无法从自定义Tableview单元格按钮删除行 - Cannot delete row from custom tableview cell button iOS-在该单元中处理按钮事件时如何访问自定义单元? - iOS - How to access custom cell when handle button event in that cell? 如何初始化自定义表格视图单元格 - How to initialize a custom tableview cell 如何在iOS中使用自定义按钮对Tableview单元进行重新排序,但不使用Move Edit图标进行重新排序 - How to Reorder tableview cell with a custom button, but not with move edit icon in ios iPhone SDK-具有自定义TextView的TableView单元格-获取新数据 - iphone sdk - TableView Cell with custom TextView - Get New Data 按下BarButtonItem之后,在每个单元格中使用一个按钮更新TableView - Update TableView With a Button in each cell after BarButtonItem pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM