简体   繁体   中英

unrecognized selector with custom tableview cell

I have a custom tableview cell. But when I want to call this cell in my cellForRowAtIndexpath, it gives the following error.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell lblPosition]: unrecognized selector sent to instance 0xa62a600'

I checked a 100 times if everthing is hooked up correctly and named correctly but still doesn't find what is wrong. This is what I am doing in code.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"KlassementCell";

    KlassementCell *cell = (KlassementCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"KlassementCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    // ask NSFetchedResultsController for the NSMO at the row in question
    Klassement *klassement = [self.fetchedResultsController objectAtIndexPath:indexPath];
    // Then configure the cell using it ...
    NSString *pointDiff = [NSString stringWithFormat:@"%@", klassement.goalsDiff];
    NSLog(@"position %@",klassement.position);
    NSLog(@"name %@",klassement.name);
    cell.lblPosition.text       = klassement.position;
    cell.lblName.text           = klassement.name;
    cell.lblgamesPlayed.text    = klassement.gamesPlayed;
    cell.lblGamesWon.text       = klassement.gamesWon;
    cell.lblGamesTied.text      = klassement.gamesTied;
    cell.lblGamesLost.text      = klassement.gamesLost;
    cell.lblGoalsPos.text       = klassement.goalsPos;
    cell.lblGoalsNeg.text       = klassement.goalsNeg;
    cell.lblGoalsDiff.text      = pointDiff;
    cell.lblPoints.text         = klassement.points;

    return cell;
}

Can anybody help?

Kind regards.

Try

if ((cell == nil) || (![cell isKindOfClass: KlassementCell.class])) {

The cell returned in the queue might not be KlassementCell.

maybe this helps in your viewDidLoad (or any other place for setup...) method:

UINib* myCellNib = [UINib nibWithNibName:@"KlassementCell" bundle:nil];
[tableView registerNib:myCellNib forCellReuseIdentifier:@"KlassementCell"];

and then instantiate the cell like this

 cell = (KlassementCell*)[myCellNib instantiateWithOwner:nil options:nil];

myCellNib should be available in the class scope

Have you set the class of 0 Index object in "KlassementCell" nib as KlassementCell and mapped all the subviews??

If not then do that and it may work.

Make sure in the nib that the class of the cell is set to "KlassementCell" AND also that the identifier is set to "KlassementCell".

You should not need to do anything else since only use 1 type of tableviewcell and only 1 identifier in cellForRowAtIndexPath

The identifier has to be the exact same as the class name, so both have to be "KlassementCell". That way there are no regular reusable UIcells in the queue. That's also why if ((cell == nil) || (![cell isKindOfClass: KlassementCell.class])) { works because you make sure you are not assigning things to properties that don't exist on a regular cell like lblPosition, lblName and lblgamesPlayed..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM