使用 UITableViewCell 时发送到实例的无法识别的选择器
unrecognized selector sent to instance when using UITableViewCell
我无法找到我在自定义单元格中放置的两个图像和文本框 "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....是主页
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'
*** 首先抛出调用栈:
任何帮助将不胜感激...我觉得我做的事情有点不对(我希望如此)。
你能尝试在 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 returns 单元格为 nil,您必须从 XIB 加载单元格。
希望上述解决方案能够解决您的问题。
你注册错了class
[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)注册class:我们分三步
1) 在表视图对象中注册我们的自定义单元格 class,如下所示
[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:注册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
文件
请按照以下 link 进行自定义单元集成。你会清楚地知道这一点。 http://code.tutsplus.com/tutorials/ios-sdk-crafting-custom-uitableview-cells--mobile-15702
现在,试着让我知道
我无法找到我在自定义单元格中放置的两个图像和文本框 "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....是主页
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' *** 首先抛出调用栈:
任何帮助将不胜感激...我觉得我做的事情有点不对(我希望如此)。
你能尝试在 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 returns 单元格为 nil,您必须从 XIB 加载单元格。
希望上述解决方案能够解决您的问题。
你注册错了class
[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)注册class:我们分三步
1) 在表视图对象中注册我们的自定义单元格 class,如下所示
[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:注册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
文件
请按照以下 link 进行自定义单元集成。你会清楚地知道这一点。 http://code.tutsplus.com/tutorials/ios-sdk-crafting-custom-uitableview-cells--mobile-15702
现在,试着让我知道