在某些属性上有错误消息。错误消息在以下代码中被注释掉。即使在其他文件中声明并合成了属性,也不明白为什么会收到此错误消息?
PlayersViewController.m
#import "PlayersViewController.h"
#import "Player.h"
#import "PlayerCell.h"
@implementation PlayersViewController
@synthesize players;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [self.players count];
}
- (UIImage *)imageForRating:(int)rating
{
switch (rating) {
case 1:return [UIImage imageNamed:@"1StarSmall.png"];
case 2:return [UIImage imageNamed:@"2StarsSmall.png"];
case 3:return [UIImage imageNamed:@"3StarsSmall.png"];
case 4:return [UIImage imageNamed:@"4StarsSmall.png"];
case 5:return [UIImage imageNamed:@"5StarsSmall.png"];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PlayerCell"];
Player *player = [self.players objectAtIndex:indexPath.row];
cell.nameLabel.text = player.name; /*property 'nameLabel' not found on object of tupe "UITableViewCell" */
cell.gameLabel.text = player.game; /*property 'gameLabel' not found on object of tupe "UITableViewCell" */
cell.ratingImageView.image = [self imageForRating:player.rating]; /*property 'ratingImageView' not found on object of tupe "UITableViewCell" */
return cell;
}
这些属性在以下两个文件中声明和实现:
PlayerCell.h
#import <Foundation/Foundation.h>
@interface Player : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *game;
@property (nonatomic, assign) int rating;
@end
PlayerCell.m
#import "Player.h"
@implementation Player
@synthesize name;
@synthesize game;
@synthesize rating;
@end
最佳答案
UITableViewCell没有名为nameLabel,gameLabel或ratingImageView的属性。如果您使用自己的自定义UITableViewCell子类,则需要告诉编译器您是在将消息发送到PlayerCell
实例而不是UITableViewCell实例。
像这样做PlayerCell *cell = (PlayerCell *) [tableView dequeueReusableCellWithIdentifier:@"PlayerCell"];
另外,您可以使用id
,如果不使用点表示法,则编译器也不会出现问题,但是为了清楚起见,我将使用显式类版本。
id cell = [tableView dequeueReusableCellWithIdentifier:@"PlayerCell"];
[cell nameLabel].text = @""; //etc
顺便说一句-如果这是您的实际代码,那么您没有实例化UITableViewCells,这也可能会带来问题。
static NSString *CellIdentifier = @"Cell";
PlayerCell *cell = (PlayerCell *)[tableView dequeueReusableCellWithIdentifier:@"PlayerCell"];
if (nil == cell){
cel = [[[PlayerCell alloc] yourInitMethodWithCellIdentifier:CellIdentifier] autorelease];
}
Player *player = [self.players objectAtIndex:indexPath.row];
cell.nameLabel.text = player.name;
cell.gameLabel.text = player.game;
cell.ratingImageView.image = [self imageForRating:player.rating];
return cell;