【问题标题】:Use of undeclared identifier 'cell' when parsing eBay API解析 eBay API 时使用未声明的标识符“单元格”
【发布时间】:2014-03-07 05:39:21
【问题描述】:

我正在尝试从 eBay 的 API 解析商品数据,但是我无法正确获取我的 tableview 设置中的单元格。我在下面的 MasterViewController 的 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中遇到多个错误,指出“单元格”是未声明的标识符。

#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

【问题讨论】:

    标签: ios objective-c json uitableview ebay-api


    【解决方案1】:

    在你的 cellForRowIndexPath 中定义 static NSString *identifire = @"Cell";

     - (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;
        }
    

    【讨论】:

    • 这是错误的。无论您是否使用静态字符串作为标识符都不会产生影响。此外,您的代码引入了一个新错误。当使用dequeueReusableCellWithIdentifier: 而不是dequeueReusableCellWithIdentifier: forIndexPath: 时,如果单元格不存在,则必须对其进行初始化。
    • @0x7fffffff 是的,你是对的,我们必须初始化单元格。我忘了提到单元格分配我编辑了我的答案请检查它
    【解决方案2】:

    您必须先在 .m 文件中导入 DetailCell.h,然后才能在代码中使用它。以便编译器识别该类。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多