【问题标题】:unrecognized selector with custom tableview cell带有自定义表格视图单元格的无法识别的选择器
【发布时间】:2012-10-08 13:17:40
【问题描述】:

我有一个自定义的表格视图单元格。但是当我想在我的 cellForRowAtIndexpath 中调用这个单元格时,它会给出以下错误。

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

我检查了 100 次,如果一切都正确连接并正确命名,但仍然没有找到问题所在。这就是我在代码中所做的。

- (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;
}

有人可以帮忙吗?

亲切的问候。

【问题讨论】:

    标签: objective-c ios ios5 uitableview


    【解决方案1】:

    试试

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

    队列中返回的单元格可能不是 KlassementCell。

    【讨论】:

    • 好吧,这行得通,但你能说为什么这有帮助吗?因为我在这个错误上搜索了几个小时
    • 我猜iOS在队列中有一个可重用的UICell(不是KlassementCell),并在您调用dequeueReusableCellWithIdentifier时提供它。但一定有一个原因,我不明白为什么它在队列中。
    【解决方案2】:

    您是否将“KlassementCell”nib中的0 Index对象的类设置为KlassementCell并映射所有子视图??

    如果没有,那就这样做,它可能会起作用。

    【讨论】:

      【解决方案3】:

      也许这对您的 viewDidLoad(或任何其他用于设置的地方...)方法有帮助:

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

      然后像这样实例化单元格

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

      myCellNib 应该在类范围内可用

      【讨论】:

        【解决方案4】:

        确保在笔尖中将单元格的类设置为“KlassementCell”并且将标识符设置为“KlassementCell”。

        您不需要做任何其他事情,因为只使用 1 种类型的 tableviewcell 并且在 cellForRowAtIndexPath 中只使用 1 个标识符

        【讨论】:

          【解决方案5】:

          标识符必须与类名完全相同,因此两者都必须是“KlassementCell”。这样队列中就没有常规的可重用 UIcell。这也是if ((cell == nil) || (![cell isKindOfClass: KlassementCell.class])) { 起作用的原因,因为您确保没有将事物分配给常规单元格上不存在的属性,例如 lblPosition、lblName 和 lblgamesPlayed..

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-07-10
            • 1970-01-01
            • 1970-01-01
            • 2016-04-06
            • 1970-01-01
            • 2020-09-11
            • 1970-01-01
            • 2012-05-26
            相关资源
            最近更新 更多