ecfedele
ecfedele

Reputation: 316

TableViewController cells not populating with data

Alright, I know I'm going to annoy someone here with my n00b-ness, so consider this a fair warning. I'm fresh-as-a-fish to Obj-C, and so what may be obvious to you most likely won't to me.

I've been following this tutorial on TableViewControllers, and I can't for the life of me get the cell titles to appear. I have cut and retailored every line of code on the site, and debugged a SIGABRT error, yet even now the data does not appear.

Here is the contents of the MasterViewController.h and /.m files, respectively:

The header file:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import <UIKit/UIKit.h>

@class DetailViewController;

@interface MasterViewController : UITableViewController <NSFetchedResultsControllerDelegate, UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) DetailViewController *detailViewController;

//  Create property "equations" as an instance of NSArray:
@property (strong, nonatomic) NSMutableArray *equations;

@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

@end

The implementation file:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "MasterViewController.h"
#import "DetailViewController.h"

@interface MasterViewController ()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end

@implementation MasterViewController

//  Synthesize NSArray instance for equation storage:
@synthesize equations = _equations;


//  Segue linking as per DetailViewController.h/.m:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showDetail"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    DetailViewController *destViewController = segue.destinationViewController;
    destViewController.equationName = [_equations objectAtIndex:indexPath.row];
}
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]     initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
self.title = @"Equations";
if (!_equations)
{
    _equations = [[NSMutableArray alloc] initWithObjects:
                  // Atomic structure equations:
                  @"Energy-Frequency Relation",
                  @"Energy-Frequency-Wavelength Relation",
                  @"Energy-Quantum Number Relation",
                  @"Momentum-Mass-Frequency",
                  @"Speed of Light Definition",
                  // Equilibrium equations:
                  @"Equilibrium Acid Constant",
                  @"Equilibrium Base Constant",
                  @"Water Equilibrium Constant",
                  @"pH Calculation",
                  @"pH-Acid Constant Relation",
                  @"pOH-Base Constant Relation",
                  @"pKa Derivation",
                  @"pKb Derivation",
                  @"pOH Calculation",
                  @"Gas-pressure Equlibrium",
                  // Gas/solution chemistry equations:
                  @"Ideal-Gas Law",
                  @"Partial-pressure equation",
                  @"Total pressure (3 partials)",
                  @"mol-Molarity Calculation",
                  @"Kelvin-Celsius Relation",
                  @"Fahrenheit-Celsius Relation",
                  @"Density Calculation",
                  @"Kinetic Energy per Molecule",
                  @"Kinetic Energy per Mol",
                  @"Molarity Equation",
                  @"Molality Equation",
                  @"Absorbance Equation",
                  @"Freezing Point Depression",
                  @"Boiling Point Elevation"
                  // Redox Equations:
                  @"Electrical current definition",
                  @"Equilibrium vs. Reduction Potential",
                  // Thermochemical relations:
                  @"Change in Free Energy",
                  @"Molar Heat Capacity",
                  @"Frequency to Rate Factor",
                  nil];
    }
}
#pragma mark - Table View

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //  Return call from NSArray *equations as to the count of elements in the table view:
    return [_equations count];
}

- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
UILabel *lblName = (UILabel *)[cell viewWithTag:100];
[lblName setText:[_equations objectAtIndex:[indexPath row]]];
return cell;
}

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

I spared y'all all 250 lines of code, cutting out only that seem to pertain to the view controller itself. Something tells me I'm simply leaving out a necessary line of code for connection, however my complete introductory status to the language as well as the lack of a debugger error doesn't trip me to it. Any ideas? Any help (nitpicking excluded) is more than appreciated and welcomed.

Upvotes: 0

Views: 135

Answers (2)

Steve Wilford
Steve Wilford

Reputation: 9002

I was able to reproduce your issue as follows:

Your current tableView:cellForRowAtIndexPath: implementation requires that the cell have a UILabel subview with a tag of 100.

UILabel *lblName = (UILabel *)[cell viewWithTag:100];

The label in the cell needs to be configured with this tag...

If you are using the "Basic" cell type in Interface Builder you have two options:

Option 1

You must select the title label and set it's tag to 100.

enter image description here

Option 2

Change the method to directly access the text label.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    [cell.textLabel setText:_equations[indexPath.row]];
    return cell;
}

You can also use an approach similar to (if not identical to) Option 2 for custom cell types where you have provided your own label.

Upvotes: 0

Xu Yin
Xu Yin

Reputation: 3940

@"Cell"that could be a couple of things you can check here.

  1. in your cellFoRowAtIndexPaht Method

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
    //add this
    if (cell == nil)
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
    
  2. double check if your table data source and table delegate is connected correctly to your table view. i think when you create uitableviewcontroller from storyboard, it should connected for you automatically, but it won't harm to double check.

Upvotes: 1

Related Questions