我有一个自定义单元格,在这个自定义单元格中我有一个按钮和一个标签:
#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/