Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Trent Burkenpas
Trent Burkenpas
22,388 Points

ios Simulator not displaying my titles

In the video "Adding a Table View Controller class" Admit sets up the app to display the blog titles. But form some reason my simulator is not displaying the titles. Here is my code

TableViewController.m

#import "TableViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.titles = [NSArray arrayWithObjects:@"The Missing Widget In the Android SDk: SmartImageView",
                  @"Get started with ios Development",
                  @"An Interveiw with Shay Howe",
                  @"Treehouse Friends: Paul Irish",
                  @"Getting A Job In Web Design and Development",
                  @"Treehouse Show Eposide 13 – LLJS, Navicons and Freamework Flights",
                  nil];

}

- (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.
    return [self.titles count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [self.titles objectAtIndex:indexPath.row];

    return cell;
}

I made sure to give the cell the identifier as cell not sure this is happening.

Thanks

2 Answers

Stone Preston
Stone Preston
42,016 Points

here you have:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    //this will give you a lot of sections in the tableview. Im not sure how the table is supposed to be set up but you most likely only want 1 section with the title rows inside that section
    return [self.titles count];
}

and in:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    //this causes you to have 0 rows, so nothing gets displayed in the table at all
    return 0;
}

these are causing your problems. You most likely want one section with all your titles in that section, so try :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.

    return self.titles.count;
}
Trent Burkenpas
Trent Burkenpas
22,388 Points

ooooooooo, ok I see what i did wrong. Thanks for the help man !