[英]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.