Dranix
Dranix

Reputation: 551

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):

*** 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

Upvotes: 0

Views: 2977

Answers (2)

meronix
meronix

Reputation: 6176

are you sure you are calling the right class? are you sure the super view class of your button is a ListCell class?

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

Upvotes: 4

The iOSDev
The iOSDev

Reputation: 5267

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

Upvotes: 1

Related Questions