ocjective-c Obtain return value from public method

Posted by Felix on Stack Overflow See other posts from Stack Overflow or by Felix
Published on 2012-06-28T14:57:25Z Indexed on 2012/06/28 15:16 UTC
Read the original article Hit count: 198

I'm pretty new to objective-C (and C in general) and iPhone development and am coming from the java island, so there are some fundamentals that are quite tough to learn for me.

I'm diving right into iOS5 and want to use storyboards.

For now I am trying to setup a list in a UITableViewController that will be filled with values returned by a web service in the future. For now, I just want to generate some mock objects and show their names in the list to be able to proceed.

Coming from java, my first approach would be to create a new Class that provides a global accessible method to generate some objects for my list:

#import <Foundation/Foundation.h>

@interface MockObjectGenerator : NSObject

+(NSMutableArray *) createAndGetMockProjects;

@end

Implementation is...

#import "MockObjectGenerator.h"

// Custom object with some fields    
#import "Project.h"

@implementation MockObjectGenerator

+ (NSMutableArray *) createAndGetMockObjects {

    NSMutableArray *mockProjects = [NSMutableArray alloc];

    Project *project1 = [Project alloc];
    Project *project2 = [Project alloc];
    Project *project3 = [Project alloc];

    project1.name = @"Project 1";
    project2.name = @"Project 2";
    project3.name = @"Project 3";

    [mockProjects addObject:project1];
    [mockProjects addObject:project2];
    [mockProjects addObject:project3];

}

And here is my ProjectTable.h that is supposed to control my ListView

#import <UIKit/UIKit.h>

@interface ProjectsTable : UITableViewController

@property (strong, nonatomic) NSMutableArray *projectsList;

@end

And finally ProjectTable.m

#import "ProjectsTable.h"
#import "Project.h"
#import "MockObjectGenerator.h"

@interface ProjectsTable {

@synthesize projectsList = _projectsList;

-(id)initWithStyle:(UITableViewStyle:style {

    self = [super initWithStyle:style];

    if (self) {

        _projectsList = [[MockObjectGenerator createAndGetMockObjects] copy];

    }

    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // only one section for all
    return 1;

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

    NSLog(@"%d entries in list", _projectsList.count);
    return _projectsList.count;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // the identifier of the lists prototype cell is set to this string value
    static NSString *CellIdentifier = @"projectCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    Project *project = [_projectsList objectAtIndex:indexPath.row];

    cell.textLabel.text = project.name
}

So while I think everything is correctly set, I expect the tableView to show my three mock objects in its rows. But it stays empty and the NSLog method prints "0 entries in list" into the console. So what am I doing wrong?

Any help is appreciated.

Best regards Felix

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about listview