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

标签 iphone ios xcode cocoa

我有一个自定义单元格,在这个自定义单元格中我有一个按钮和一个标签:

#import <UIKit/UIKit.h>

@interface ListCell : UITableViewCell{

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

@end

当我处理按钮的点击事件时:

- (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;
}

当我尝试从我的自定义单元格 (ListCell) 访问任何项目时抛出异常:

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

我认为我获取自定义单元格的方式是错误的。有谁知道如何正确获取它?
提前致谢

最佳答案

你确定你调用的是正确的类吗? 您确定按钮的 super View 类是 ListCell 类吗?

尝试检查一下:

    // (...)
    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!!!");
    }

关于iphone - iOS - 如何在处理该单元格中的按钮事件时访问自定义单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10138552/

相关文章:

objective-c - 将 2 个 UIImages 保存为一个,同时保存旋转、调整大小信息及其质量

android - 如何清除flutter项目缓存?

objective-c - 在 objective-c 中的两个 View Controller 之间共享对象

iphone - 在 View Controller 的宽度上水平均匀分布 UIButton 的最简单方法?

iphone - 进入后台取消剩余请求

ios - 如何在ios sdk中更改应用程序内的应用程序语言

iphone - Facebook SDK 和 iOS 应用程序 (authButtonAction) 的问题

iphone - 如何在 UITableView 中定位无标题部分

ios - 为什么我会收到此错误 : Result values in '? :' expression have mismatching types '[String]?' and 'String'

iphone - 退出应用程序后 imageview 为空