Reputation: 35
I am not able to locate the two images and the text box that I placed in my custom cell "GameTableCell.H"... not sure what I'm doing wrong here.
GameTableCell.H:
//
#import <UIKit/UIKit.h>
@interface GameTableCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *GameTime;
@property (strong, nonatomic) IBOutlet UIImageView* AwayImage;
@property (strong, nonatomic) IBOutlet UIImageView* HomeImage;
@end
GameTableCell.m:
//
#import "GameTableCell.h"
@implementation GameTableCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
I have connected the items was well...
Calling the GameTableCell....is HomePage
HomePage.H:
#import <UIKit/UIKit.h>
@interface HomePage : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *PendingChal;
@property (strong, nonatomic) IBOutlet UITableView *ActiveChal;
@property (nonatomic, strong)NSArray *HomeImages;
@property (nonatomic, strong)NSArray *AwayImages;
@property (nonatomic, strong)NSArray *SportGameInfo;
@end
HomePage.m:
//
#import "HomePage.h"
#import "PickSport.h"
#import "GameTableCell.h"
@interface HomePage ()
@end
@implementation HomePage
@synthesize ActiveChal,PendingChal;
-(IBAction)makeChallenge:(id)sender{
PickSport *second = [[PickSport alloc] initWithNibName:@"PickSport" bundle:nil];
[self presentViewController:second animated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Arrays for filling table cells
_HomeImages=@[@"ic_bengals_nfl",@"ic_bengals_nfl",@"ic_bengals_nfl",@"ic_bengals_nfl", ];
_AwayImages=@[@"ic_bears_nfl",@"ic_bears_nfl",@"ic_bears_nfl",@"ic_bears_nfl", ];
_SportGameInfo=@[@"7:00pm",@"8:00pm",@"9:00pm",@"10:00pm",];
[self.PendingChal registerClass: [UITableViewCell class]forCellReuseIdentifier:@"GameTableCell"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Set up the table props
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
//number of rows in the table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// rows eqiv to length of array SportGameinfo
return _SportGameInfo.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier=@"GameTableCell";
GameTableCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if(tableView == PendingChal){
if (!cell) {
NSLog(@"cell was nil");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
int row =[indexPath row];
cell.HomeImage.image=[UIImage imageNamed:_HomeImages[row]];
cell.AwayImage.image=[UIImage imageNamed:_AwayImages[row]];
cell.GameTime.text=_SportGameInfo[row];
}
if(tableView == ActiveChal){
int row =[indexPath row];
cell.GameTime.text=_SportGameInfo[row];
cell.HomeImage.image=[UIImage imageNamed:_HomeImages[row]];
cell.AwayImage.image=[UIImage imageNamed:_AwayImages[row]];
}
return cell;
}
The specific error reads: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell HomeImage]: unrecognized selector sent to instance 0x79ee11a0' *** First throw call stack:
Any help would be appreciated... I feel I'm doing something just slightly wrong (I hope).
Upvotes: 0
Views: 3206
Reputation: 2862
you are registering wrong class
[self.PendingChal registerClass: [UITableViewCell class]forCellReuseIdentifier:@"GameTableCell"];
should changed like
[self.PendingChal registerClass: [GameTableCell class]forCellReuseIdentifier:@"GameTableCell"];
And please following method in your GameTableCell.m file
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"GameTableCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
self = [topLevelObjects objectAtIndex:0];
return self;
}
This resolve your issue. For clear idea I am adding some more info please go through once Basically we have two ways to implement custom cells.
CASE1)registering class: we have three steps
1)register our custom cell class in tableview object like bellow
[self.PendingChal registerClass: [UITableViewCell class]forCellReuseIdentifier:@"GameTableCell"];
2)And we need to manually initilize customcell with specific nib in customcell .m file like bellow.
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"GameTableCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
self = [topLevelObjects objectAtIndex:0];
return self;
}
3) You need not to check cell value nil in cellForRowAtIndexPath, you will get all references automatically
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier=@"GameTableCell";
GameTableCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
//Access all cell properties here.
return cell.
}
CASE2: Register nib.
[self.PendingChal registerNib:[UINib nibWithNibName:@"GameTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"GameTableCell"];
and use directly in your cellForRowAtIndexPath
method like bellow
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier=@"GameTableCell";
GameTableCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
//Access all cell properties here.
return cell.
}
Finally instead of using
static NSString *CellIdentifier=@"GameTableCell";
GameTableCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
use following method
static NSString *CellIdentifier=@"GameTableCell";
GameTableCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier forIndexPath:indexPath
];
Note: overRide customcell.m
file according to this method
Please go through with following link for custom cell integration. you will get clear idea about this. http://code.tutsplus.com/tutorials/ios-sdk-crafting-custom-uitableview-cells--mobile-15702
Now, try and let me know
Upvotes: 3
Reputation: 10354
Can you try replacing this in tableView: cellForRowAtIndexPath:
if (!cell) {
NSLog(@"cell was nil");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
with
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"GameTableCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
}
It looks like you are initialising the cell as UITableViewCell which doesn't contain reference for HomeImage variable, so you have to load the cell from XIB if dequeueReusableCellWithIdentifier returns cell as nil.
Hope the above solution will resolve your issue.
Upvotes: 1