看不到属性错误

时间:2011-12-21 22:17:38

标签: ios objective-c xcode cocoa

在某些属性上有错误消息。以下代码中注释掉了错误消息。不明白为什么我收到此错误消息,即使在其他文件中声明和合成属性?

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

2 个答案:

答案 0 :(得分:2)

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;

答案 1 :(得分:1)

您有一个名为“PlayerCell.m”的文件,它创建的“Player”实例为NSObject。相反,你应该让“PlayerCell.m”创建“PlayerCell”的实例,它应该是UITableViewCell的子类。

从那里PlayerCell应该有UILabel *nameLabelUILabel *gameLabel等属性。

你给出的名为“PlayerCell.h”和“PlayerCell.m”的片段看起来很好,如果它们实际上是“Player.h”和“Player.m”而已。

相关问题