本文介绍了使用 UITableViewCell 时发送到实例的无法识别的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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:

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

调用 GameTableCell....is HomePage

Calling the GameTableCell....is 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

主页.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"*** 第一次抛出调用栈:

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).

推荐答案

您注册了错误的课程

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

应该像

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

请在您的 GameTableCell.m 文件中使用以下方法

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)注册课程:我们分为三个步骤

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.
}

CASE2:注册笔尖.

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

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

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.
    }

最后代替使用

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

使用以下方法

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

];

注意:按照这个方法覆盖customcell.m文件

Note: overRide customcell.m file according to this method

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

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

现在,试着告诉我

这篇关于使用 UITableViewCell 时发送到实例的无法识别的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 09:51