Search code examples
iphoneios5xcode4

tableviewcellForRowAtIndexPath is not being called


I have this basic shopping list app where in the first view there are two text fields. One that is the name of the item and the other is the item quantitiy. Once the user types those two entries and presses the save button, the app segues to the table view controller where the entries are stored first into an object called item, and then the object is stored into an array of items. Then I implement all the required table view methods and have the item displayed in a table view. Sadly, nothing is being displayed. When I save an entry the table view appears blank. After doing some debugging with NSLog's, I figured out that the crucial method tableviewcellForRowAtIndexPath is not being called. I found that others had a similar question and that the solution was to set the tableviewcontroller as the delegate, I tried that but it didn't work. Do you know what's wrong with my code? Thanks.

Here is my view controller for the first image, then I will show you my tableviewController code

   #import "TVViewController.h"
#import "TableViewController.h"
//#import "TableViewController.m"

@implementation TVViewController
@synthesize title;//one of the text fields
@synthesize subtitle;//the other text field

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Save"]) {
        NSString *string1 = self.title.text;
        NSString *string2 = self.subtitle.text;
        [segue.destinationViewController addName: string1 andNumber:string2];
    }
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setTitle:nil];
    [self setSubtitle:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

That is my viewcontroller This is my table view controller, ignore the nslog's those were for debugging purposes

 #import "TableViewController.h"
#import "Item.h"

@implementation TableViewController

@synthesize items;

-(void) addName:(NSString *) name
      andNumber:(NSString *) numberOfItems
{
    Item *item = [[Item alloc] init];
    item.itemName = name;
    item.itemQuantity = numberOfItems;
    [self.items addObject:item];
    NSLog(@"Data has been passed");

}


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {

    }

    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSLog(@"VIew is ready");
    //[self.tableView reloadData];



    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"list initialized by count");
    // Return the number of rows in the section.

    return [self.items count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Item in cart";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...

    Item *item = [self.items objectAtIndex:indexPath.row];
    cell.textLabel.text = item.itemName;
    cell.detailTextLabel.text = item.itemQuantity;
    return cell;

}

Solution

  • as far as I remember you need to set tableVeiw's dataSource for this, delegate is for recieveing events

    http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDataSource