【问题标题】:unrecognized selector sent to instance when using UITableViewCell使用 UITableViewCell 时发送到实例的无法识别的选择器
【发布时间】:2015-06-20 21:33:35
【问题描述】:

我无法找到我放置在自定义单元格“GameTableCell.H”中的两个图像和文本框...不知道我在这里做错了什么。

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

我已经连接的项目很好...

调用GameTableCell....是主页

主页.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

主页.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;
}

具体错误如下: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UITableViewCell HomeImage]:无法识别的选择器发送到实例 0x79ee11a0” *** 首先抛出调用栈:

任何帮助将不胜感激......我觉得我做的事情有点不对(我希望)。

【问题讨论】:

  • 请添加更多标签,例如编程语言和与您的查询相关的其他标签
  • @Anand 已添加,谢谢!
  • 你使用 Images.xcassets 添加图像???还要添加异常断点
  • 有没有达到"cell was nil" 部分?我建议你不要分配UITableViewCell,而是分配GameTableCell
  • 是的,我正在从 xcassettes 中提取它们。最终我会从一个我试图让这个“虚拟”表首先在本地运行的 url 中提取,然后再进入互联网的深处。

标签: ios objective-c uitableview unrecognized-selector


【解决方案1】:

你注册错了课程

 [self.PendingChal registerClass: [UITableViewCell class]forCellReuseIdentifier:@"GameTableCell"];

应该像

这样改变
[self.PendingChal registerClass: [GameTableCell class]forCellReuseIdentifier:@"GameTableCell"];

请按照您的 GameTableCell.m 文件中的方法进行操作

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

这解决了您的问题。为了清楚起见,我要添加更多信息,请浏览一次 基本上我们有两种方法来实现自定义单元格。

CASE1)注册类:我们分为三个步骤

1)在tableview对象中注册我们的自定义单元类,如下所示

[self.PendingChal registerClass: [UITableViewCell class]forCellReuseIdentifier:@"GameTableCell"];

2)我们需要在 customcell .m 文件中使用特定的 nib 手动初始化 customcell,如下所示。

-(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) cellForRowAtIndexPath 中的单元格值不需要检查 nil,你会自动获取所有引用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier=@"GameTableCell";
    GameTableCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

//Access all cell properties here.

return cell.
}

案例2:注册笔尖。

[self.PendingChal registerNib:[UINib nibWithNibName:@"GameTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"GameTableCell"];

并直接在您的cellForRowAtIndexPath 方法中使用,如下所示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier=@"GameTableCell";
        GameTableCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

    //Access all cell properties here.

    return cell.
    }

终于不用了

static NSString *CellIdentifier=@"GameTableCell";
            GameTableCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

使用以下方法

static NSString *CellIdentifier=@"GameTableCell";
                GameTableCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier forIndexPath:indexPath

];

注意:按此方法覆盖customcell.m文件

请通过以下链接进行自定义单元集成。你会对此有清晰的认识。 http://code.tutsplus.com/tutorials/ios-sdk-crafting-custom-uitableview-cells--mobile-15702

现在,试着告诉我

【讨论】:

  • 我试过这个......它让我通过错误。但是我的表中没有任何内容。此外,我没有收到任何项目为 Null 的错误...
  • 嗨,你可以试试 [self.PendingChal registerNib:[UINib nibWithNibName:@"GameTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"GameTableCell"]; // 你的xib类名
  • 如果上面编辑的我的解决方案不起作用,请告诉我。我们还有另一种解决方案
【解决方案2】:

你可以尝试在tableView: cellForRowAtIndexPath:中替换它

if (!cell) {
    NSLog(@"cell was nil");
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

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

看起来您正在将单元格初始化为 UITableViewCell,它不包含对 HomeImage 变量的引用,因此如果 dequeueReusableCellWithIdentifier 返回单元格为 nil,则必须从 XIB 加载单元格。

希望以上解决方案能解决您的问题。

【讨论】:

  • hmmmm 没有解决问题。同样的问题仍然存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 2012-07-24
相关资源
最近更新 更多