【问题标题】:UITableViewCell subclass throwing an unrecognized selector sent to instanceUITableViewCell 子类抛出一个无法识别的选择器发送到实例
【发布时间】:2015-09-11 13:11:48
【问题描述】:

我的 UITableViewCell 给出了一个 -[UITableViewCell nameTeam]: unrecognized selector sent to instance。

我创建了一个带有 UITableViewCell 和 UILabel 的 PlayerStatsTableViewCell.xib。将自定义类设置为“PlayerStatsTableViewCell”,并将其表视图单元格标识符设置为“playerStats”

PlayerStatsTableViewCell.h

@interface PlayerStatsTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *nameTeam;
@end

PlayerStatsTableViewCell.m

@implementation PlayerStatsTableViewCell
@synthesize nameTeam;

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

在我的 PlayerStatsTableViewController 中,cell.nameTeam 抛出错误

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PlayerStatsTableViewCell *cell = (PlayerStatsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"playerStats"];
    [tableView registerNib:[UINib nibWithNibName:@"PlayerStatsTableViewCell" bundle:nil] forCellReuseIdentifier:@"playerStats"];

    cell.nameTeam.text = @"PLEASE";

    return cell;
}

PlayerStatsTableViewCell.xib 显示自定义类和标识符

错误信息

来自 Storyboard 的 PlayerStatsTableViewController

【问题讨论】:

  • 不需要写"[tableView registerNib:[UINib nibWithNibName:@"PlayerStatsTableViewCell" bundle:nil] forCellReuseIdentifier:@"playerStats"];"

标签: uitableview xib unrecognized-selector


【解决方案1】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PlayerStatsTableViewCell *cell = (PlayerStatsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"playerStats"];
    if (cell == nil) {
          NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PlayerStatsTableViewCell" owner:self options:nil];
          cell = (PlayerStatsTableViewCell *)[nib objectAtIndex:0];
    }

    cell.nameTeam.text = @"PLEASE";

    return cell;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    相关资源
    最近更新 更多