Noam Solovechick
Noam Solovechick

Reputation: 1147

TableView reloadData doesn't call cellForRowAtIndexPath

I have two view controllers/ 1. SetupRingBoardViewController 2. SetupRingBoard*ADD*ViewController

the first view controller is UITableViewController. when we first launch the view - the ViewController has a 1 fixed section with a 1 fixed row. In that ViewController, there is a UIBarButton that is calling the SetupRingBoardADDViewController (modal - default, I'm using storyboard).

the second view controller is UIView controller. this viewController contains a UITableView and a UINavigationBar. the UITableView is actually a one big form, that the user can enter data into it. the UINavigationBar contains an 'Add' UIBarButton. When this button is being pressed, the method 'addButton' is being called.

The 'addButton' method should refresh the UITableView in the SetupRingBoardViewController. In the end, after pressing the 'addButton' button - there should be 2 sections in the SetupRingBoardViewController's UITableView: 1. The fixed section with 1 row in it. 2. A section with X rows in it, each row will have a title: @"A Row!"; (X = the number of 'addButton' being pressed).

Finally, here's the code:

SetupRingBoardViewController.h :

//
//  SetupRingBoardViewController.h
// 
//
//  Created by  on 12/24/12.
//  Copyright (c) 2012 Noam. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "SetupRingBoardADDViewController.h"
//#import "StudyHour.h"

@interface SetupRingBoardViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *listOfStudyHours;

@end

SetupRingBoardViewController.m :

//
//  SetupRingBoardViewController.m
//  
//
//  Created by on 12/24/12.
//  Copyright (c) 2012 Noam. All rights reserved.
//

#import "SetupRingBoardViewController.h"
#import "SetupEmptyListViewController.h"
@interface SetupRingBoardViewController ()

@end

@implementation SetupRingBoardViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id)init
{
    self = [super init];
    if(self != nil)
    {
        if(!_listOfStudyHours) _listOfStudyHours = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if(_listOfStudyHours) NSLog(@"%@",_listOfStudyHours);
    [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;

    //[self.tableView reloadData];
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    if(![_listOfStudyHours count])
    {
        NSLog(@"numberOfSectionsInTableView: 1");
        return 1;
    }
    else return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if(section == 0) return 1;
    else return [_listOfStudyHours count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(![indexPath section])
    {
        NSLog(@"It got to the first");
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        // Configure the cell...

        return cell;
    }
    else
    {
        NSLog(@"It got to the second");
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        cell.textLabel.text = [_listOfStudyHours objectAtIndex:indexPath.row];
        return cell;
    }
}

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

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [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;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

SetupRingBoardADDViewController.h :

//  SetupRingBoardADDViewController.h
//  
//
//  Created by on 12/26/12.
//  Copyright (c) 2012 Noam. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "SetupRingBoardViewController.h"
#import "UITableViewCell+Checkmark.h"
//#import "StudyHour.h"

@interface SetupRingBoardADDViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

- (IBAction)addButton:(id)sender;
@end

SetupRingBoardADDViewController.m :

- (IBAction)addButton:(id)sender {
        SetupRingBoardViewController *rbVC = [[SetupRingBoardViewController alloc] init];
        [rbVC.listOfStudyHours addObject:@"A row!"];
        NSLog(@"%@",rbVC.listOfStudyHours);
        [[rbVC tableView] reloadData];
        [self dismissViewControllerAnimated:YES completion:nil];
}

(This is not the whole code, but it is the only thing that is relevant.)

The problem is that the method cellForRowAtIndexPath is not being called when I call the [tableView reloadData].

I hope you'll help me, I'm trying to figure it out for a long time :/

Upvotes: 0

Views: 3326

Answers (2)

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

Please check if you have set/connected Delegate and Datasource to the File's Owner.

And check the array count of your model, if it contains value of not?

NSLog in the numberOfRowsInSection method and check it by using breakpoints and step over.

Upvotes: 5

rdelmar
rdelmar

Reputation: 104082

I think you need to make 2 changes. One, put the creation of the array in viewDidLoad instead of init (neither initWithStyle: nor init will be called if you created your table view controller in a storyboard -- initWithCoder: will be, so you could use that instead of viewDidLoad):

- (void)viewDidLoad
{
    [super viewDidLoad];
    if(!_listOfStudyHours) _listOfStudyHours = [[NSMutableArray alloc] init];
    if(_listOfStudyHours) NSLog(@"%@",_listOfStudyHours);
    [self.tableView reloadData];
}

Secondly, In your button method, you need to go back to the same instance that you came from, not create a new one. You can use the presentingViewController property to do that:

- (IBAction)addButton:(id)sender {
    SetupRingBoardViewcontroller *rbVC = (SetupRingBoardViewcontroller *)self.presentingViewController;
    [rbVC.listOfStudyHours addObject:@"A row!"];
    NSLog(@"%@",rbVC.listOfStudyHours);
    [[rbVC tableView] reloadData];
    [self dismissViewControllerAnimated:YES completion:nil];
}

Upvotes: 3

Related Questions