2014-01-29 55 views
1

我的问题与this one类似,但我感觉不同,所以我需要在这里提出问题。iOS - 在'NSArray'类型的对象上找不到属性“name”

我正在创建我的第一个iOS应用程序,一个基本的主 - 细节应用程序,并使用JSONModel来提取API,但我认为在这种情况下并不多。我有一个UITableView,其中每一行代表一个拳击手,每个拳击手都有一个名称,重量级别,记录等,这一切都很好。我遇到问题的地方是在各个详细信息页面上显示该数据。主要是我无法弄清楚我应该如何在segue中传递数据,我知道我想使用该行来传递关联的boxer对象,但似乎无法找出正确的方法。在我的代码中,我有NSArray,但也尝试过NSMutableArray和NSObject,每个都有相同的错误:在'NSW whatever'类型的对象上找不到属性'name'。任何帮助,将不胜感激。谢谢。

BoxerMasterViewController.m

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
Boxer* boxerDetails = _feed.boxers[indexPath.row]; 

BoxerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BoxerCell" forIndexPath:indexPath]; 
cell.boxerNameLabel.text = [NSString stringWithFormat:@"%@", boxerDetails.name]; 
} 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
if ([[segue identifier] isEqualToString:@"showBoxerDetail"]) { 
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    NSArray *object = _feed.boxers[indexPath.row]; 
    [[segue destinationViewController] setBoxerDetailItem:object]; 

} 
} 

BoxerDetailViewController.h

#import <UIKit/UIKit.h> 
@interface BoxerDetailViewController : UIViewController 
@property (strong, nonatomic) NSArray *boxerDetailItem; 
@property (weak, nonatomic) IBOutlet UILabel *boxerName; 
@end 

BoxerDetailViewController.m

self.boxerName.text = [self.boxerDetailItem.name]; 

回答

4

Boxer类型的对象上使用boxerDetails.name,那是你想要的类型boxerDetailItem

@property (strong, nonatomic) Boxer *boxerDetailItem; 

使用此行的代码不同的两种情况:

Boxer* boxerDetails = _feed.boxers[indexPath.row]; 

NSArray *object = _feed.boxers[indexPath.row]; 

的对象是一个义和拳或一个NSArray。你决定哪个。 (我的猜测是义和团)

这是我对你的代码的解释:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
Boxer* boxerDetails = _feed.boxers[indexPath.row]; 

BoxerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BoxerCell" forIndexPath:indexPath]; 
cell.boxerNameLabel.text = [NSString stringWithFormat:@"%@", boxerDetails.name]; 
} 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
if ([[segue identifier] isEqualToString:@"showBoxerDetail"]) { 
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    Boxer *object = _feed.boxers[indexPath.row]; 
    [[segue destinationViewController] setBoxerDetailItem:object]; 

} 
} 

@interface BoxerDetailViewController : UIViewController 
@property (strong, nonatomic) Boxer *boxerDetailItem; 
@property (weak, nonatomic) IBOutlet UILabel *boxerName; 
@end 
+0

eugh,你是对的,那会教我跑遍百家教程,看起来每个代码都需要一些代码。我要解决我的问题,如果没有错误将其标记为正确的答案。谢谢! –

+0

这是你没有列出的代码的一部分,所以我没有建议你在那里。 – Putz1103

1

你想“boxerDetailItem”的类型为“义和团”的,因为你是在一个单一的“义和团”对象传递不“义和团”对象的“NSArray”。

相关问题