使用UITableViewCell

时间:2015-06-20 12:13:13

标签: ios objective-c uitableview unrecognized-selector

我无法找到我放入自定义单元格“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

我连接的物品很好...... Connecting GameTableCell

调用GameTableCell ....是HomePage

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

HomePage.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' ***首先抛出调用堆栈:

任何帮助都会受到赞赏......我觉得我做的事情略有不妥(我希望)。

2 个答案:

答案 0 :(得分:3)

你正在注册错误的课程

 [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对象中注册我们的自定义单元类,如bellow

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

2)我们需要手动使用customcell .m文件中的特定笔尖初始化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:注册nib。

[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

现在,试着让我知道

答案 1 :(得分:1)

您可以尝试在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加载单元格。

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