简体   繁体   English

iOS-在该单元中处理按钮事件时如何访问自定义单元?

[英]iOS - How to access custom cell when handle button event in that cell?

I have a custom cell, and in this custom cell I have a button and a label: 我有一个自定义单元格,在这个自定义单元格中,我有一个按钮和一个标签:

#import <UIKit/UIKit.h>

@interface ListCell : UITableViewCell{

}
@property (nonatomic, retain) IBOutlet UIButton* buttonContent;
@property (nonatomic, retain) IBOutlet UILabel* detailContent;

@end

When I handle button's click event: 当我处理按钮的点击事件时:

- (IBAction)expandListCell:(id)sender{
    UIButton *button = (UIButton *)sender;
    ListCell *cell = (ListCell *)button.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    cell.detailContent.text = @"FALSE"; // It throw an exception   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"listCell";
    ListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];   
    cell.buttonContent.tag = indexPath.row;
    return cell;
}

It throw an exception when I try to access any item from my custom cell(ListCell): 当我尝试从自定义单元格(ListCell)访问任何项目时,它将引发异常:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView detailContent]: unrecognized selector sent to instance 0x8887ca0'

I think the way I got custom cell is wrong. 我认为获取自定义单元格的方式是错误的。 Anyone know how to get it properly? 有人知道如何正确获取吗?
Thank in advance 预先感谢

are you sure you are calling the right class? 您确定自己正在上正确的课吗? are you sure the super view class of your button is a ListCell class? 您确定按钮的超级视图类是ListCell类吗?

try to check this : 尝试检查一下:

    // (...)
    ListCell *cell = (ListCell *)button.superview;
    if ([cell.class isSubclassOfClass:[ListCell class]]) {
        ///// just a check for indexPath: /////
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        NSLog(@"current indexPath: %@", indexPath );
        ///////////// END CHECK ///////////////
        cell.detailContent.text = @"FALSE"; 
    }else{
        NSLog(@"NOT THE RIGHT CLASS!!!");
    }

ok i got it sorry not getting your question properly what you can do is just assign a teg to your label view before add it to the cell and then at the time of retrive just use 好吧,我很抱歉没有正确地回答您的问题,您可以做的就是在将标签添加到单元格之前先将其分配给标签视图,然后在检索时使用

UILabel *name = (UILabel *)[cell viewWithTag:kLabelTag];

and set the text for label 并设置标签文本

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

相关问题 单击该单元格中的按钮时如何更改自定义单元格中的标签文本 - how to change label text in custom cell when button in that cell clicked 如何在iOS中使用自定义按钮对Tableview单元进行重新排序,但不使用Move Edit图标进行重新排序 - How to Reorder tableview cell with a custom button, but not with move edit icon in ios 如何在iOS中的自定义表格单元中创建Button动作 - How to create a Button action inside custom table cell in ios Iphone UITableView自定义单元格按钮事件 - Iphone UITableView custom cell button event 如何从按钮按下事件中获取单元格数据(自定义表格视图单元格) - How to get cell data from button pressed event (Custom tableview cell) 如何通过使用单元格中的自定义按钮删除UITableView中的单元格? - How to delete a cell in UITableView by using custom button in cell? 如何禁用 UITableview 单元格的 UserInteraction 但不在单元格上的自定义按钮中 - how to disable UserInteraction for UITableview cell but not in custom button on cell 如何在iOS自动化中访问UITableView内部的单元格 - How to access cell inside UITableView in iOS automation 如何在iOS中以编程方式在自定义单元格中设置自动版式 - How to set AutoLayout in Custom cell By Programmatically in ios iOS - 如何在自定义单元格中使用继承? - iOS - How to use inheritance in custom cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM