2014-03-07 48 views
0

我試圖從eBay的API解析項目數據,但是我無法正確地獲取tableview設置中的單元格。我在我的MasterViewController的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中收到多個錯誤,指出'cell'是一個未聲明的標識符。解析eBay API時使用未聲明的標識符'cell'

#import "XYZMasterViewController.h" 
#import "XYZItem.h" 
#import "XYZItemManager.h" 
#import "XYZItemCommunicator.h" 

@interface XYZMasterViewController() <XYZItemManagerDelegate> { 
    NSArray *_items; 
    XYZItemManager *_manager; 
} 
@end 

@implementation XYZMasterViewController 

- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    _manager = [[XYZItemManager alloc] init]; 
    _manager.communicator = [[XYZItemCommunicator alloc] init]; 
    _manager.communicator.delegate = _manager; 
    _manager.delegate = self; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(startFetchingItems:) 
               name:@"kCLAuthorizationStatusAuthorized" 
               object:nil]; 


    self.navigationItem.leftBarButtonItem = self.editButtonItem; 

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)]; 
    self.navigationItem.rightBarButtonItem = addButton; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return _items.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

    Item *item = _items[indexPath.row]; 
    [cell.titleLabel setText:item.title]; 
    [cell.priceLabel setText:item.price]; 

    return cell; 
} 


- (void)didReceiveItems:(NSArray *)items 
{ 
    _items = items; 
    [self.tableView reloadData]; 
} 

- (void)fetchingItemsFailedWithError:(NSError *)error 
{ 
    NSLog(@"Error %@; %@", error, [error localizedDescription]); 
} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)insertNewObject:(id)sender 
{ 
    if (!_objects) { 
     _objects = [[NSMutableArray alloc] init]; 
    } 
    [_objects insertObject:[NSDate date] atIndex:0]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

#pragma mark - Table View 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return _objects.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

    NSDate *object = _objects[indexPath.row]; 
    cell.textLabel.text = [object description]; 
    return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [_objects removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
    } 
} 

/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"showDetail"]) { 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     NSDate *object = _objects[indexPath.row]; 
     [[segue destinationViewController] setDetailItem:object]; 
    } 
} 

@end 

XYZItem.h:

#import <Foundation/Foundation.h> 

@interface Group : NSObject 
@property (strong, nonatomic) NSString *title; 
@property (strong, nonatomic) NSString *price; 
@end 

XYZItemManager.h:

#import <Foundation/Foundation.h> 

#import "XYZItemManagerDelegate.h" 
#import "XYZItemCommunicatorDelegate.h" 

@class XYZItemCommunicator; 

@interface XYZItemManager : NSObject<XYZItemCommunicatorDelegate> 
@property (strong, nonatomic) XYZItemCommunicator *communicator; 
@property (weak, nonatomic) id<XYZItemManagerDelegate> delegate; 

@end 

XYZItemCommunicator.h:

#import <Foundation/Foundation.h> 

@protocol XYZItemCommunicatorDelegate; 

@interface XYZItemCommunicator : NSObject 
@property (weak, nonatomic) id<XYZItemCommunicatorDelegate> delegate; 

@end 

回答

0

必須導入DetailCell.h你。 m文件,然後在代碼中使用它。以便編譯器將識別該類。

#import "DetailCell.h" 

    #import "XYZMasterViewController.h" 
    #import "XYZItem.h" 
    #import "XYZItemManager.h" 
    #import "XYZItemCommunicator.h" 
1

定義static NSString *identifire = @"Cell";在cellForRowIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *identifire = @"Cell"; 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifire]; 

     NSDate *object = _objects[indexPath.row]; 
     cell.textLabel.text = [object description]; 
     return cell; 
    } 
+0

這是不對的。無論您是否使用靜態字符串作爲標識符都不會產生影響。另外,你已經爲你的代碼引入了一個新的bug。當使用'dequeueReusableCellWithIdentifier:'而不是'dequeueReusableCellWithIdentifier:forIndexPath:'時,如果單元格不存在,則必須初始化它們。 –

+1

@ 0x7fffffff是的,你是對的,我們必須初始化單元格。我忘了提到單元格分配,我編輯了我的答案,請檢查它 – morroko