2011-12-21 99 views
0

在某些屬性上有錯誤消息。錯誤消息在以下代碼中註釋掉。不明白爲什麼我會收到此錯誤消息,即使屬性是在其他文件中聲明和合成的?看不到屬性錯誤

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

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; 
+0

謝謝你的解釋。這有效;再次感謝! – pdenlinger 2011-12-21 22:31:31

1

您有一個名爲「PlayerCell.m」文件創建「播放器」,這是一個NSObject的實例。相反,您應該讓「PlayerCell.m」創建「PlayerCell」的實例,該實例應該是UITableViewCell的一個子類。

從那裏,PlayerCell應該具有的屬性UILabel *nameLabelUILabel *gameLabel

你給你所謂的「PlayerCell.h」和「PlayerCell.m」的片段看起來不錯,如果他們實際上是「Player.h」和「Player.m」代替。