skip to Main Content

I’m attempting to parse item data from eBay’s API, however I’m having trouble getting the cells in my tableview setup properly. I’m getting multiple errors in the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method of my MasterViewController below stating that ‘cell’ is an undeclared identifier.

#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

2

Answers


  1. You must import the DetailCell.h in your .m file , before using it in your code. so that the compiler will identify that class.

      #import "DetailCell.h"
    
      #import "XYZMasterViewController.h"
      #import "XYZItem.h"
      #import "XYZItemManager.h"
      #import "XYZItemCommunicator.h"
    
    Login or Signup to reply.
  2. Define static NSString *identifire = @"Cell"; in your 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;
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search