ios stuck at an exception while subclassing uitableviewcell
-
21-07-2021 - |
Question
I am trying to build a custom cell for UITableview. The approach I have taken is
Subclassed the
UITableViewCell
and created my own custom class which has members for UI components (Labels, images etc)Created a nib and changed its class to my custom class.
Here is the code of my class
Header file
#import <UIKit/UIKit.h>
@interface ActivePoolViewCustomCell : UITableViewCell{
UILabel* lblDate;
}
@property (nonatomic, retain)IBOutlet UILabel* lblDate;
@end
Implementation file
#import "ActivePoolViewCustomCell.h"
@implementation ActivePoolViewCustomCell
@synthesize lblDate;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
Then I created a nib and changed its class to ActivePoolViewCustomCell and connected a UILabel to the lblDate outlet.
Now, the problem I face is, when I execute the following code to load the nib
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"ActivePoolCell";
ActivePoolViewCustomCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ActivePoolViewCustomCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = (ActivePoolViewCustomCell*)[topLevelObjects objectAtIndex:0];
cell.lblDate.text = @"abcd";
}
return cell;
}
I get the following error
2012-10-30 18:13:56.451 Cda[637:f803] * **Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key lblDate.'
After a lot of struggle, I am unable to comprehend the cause of this problem. What is the problem here ? or the concept itself is wrong ?
Solution
I finally sorted it out. The problem was, I was applying the ActivePoolViewCustomCell class to the files owner of my custom cell nib which I should have been applying to the cell itself.
this video helped me out to resolve it