Nick
Nick

Reputation: 5872

How do I bind an array of NSObjects to a UITableView?

I am very new to Objective-C and Xcode and was hoping somebody might be able to give me a little assistance with a basic programming issue.

I am looking to add TranslationRegions (a custom data model) to a stack and call it for use within a controller.

The array should be:

  1. Loaded when the app starts
  2. Available to call at any time, anywhere in the application

Eventually, array items will be loaded via a call to a web service, but for now I'd like to load items in my model via code.

I have created an object (TranslationRegion) with .h and .m files, which I wish to use to bind a table with.

I may be going massively wrong here so any advice will be appreciated.

Translation region currently only has one property descriptionOfRegion.

I have included my controller first, to show what I'm trying to do, followed by my .h and .m files for my model.

Actual Controller

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"Show Regions"])
        {
            // Need to instantiate the array in here and pass it to the 'setRegions' within the Table View Controller
            //[segue.destinationViewController setRegions:regionArray];
        }
    }

Table View Controller

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyApp Regions";

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

// Configure the region cell
id region = [self.regions objectAtIndex:indexPath.row];
cell.textLabel.text = [@"" stringByAppendingString:[TranslationRegion descriptionOfRegion:region]];

return cell;
}

TranslationRegion.h

#import <Foundation/Foundation.h>

@interface TranslationRegion : NSObject

@property (nonatomic, readonly) id region;

+ (NSString *)descriptionOfRegion:(id)region;

@end

TranslationRegion.m

#import "TranslationRegion.h"

@interface TranslationRegion()
  @property (nonatomic, strong) NSMutableArray *regionStack;
@end

@implementation TranslationRegion

@synthesize regionStack = _regionStack;

- (NSMutableArray *)regionStack
{
    if (_regionStack == nil) _regionStack = [[NSMutableArray alloc] init];
    return _regionStack;
}

- (id)region
{
    return [self.regionStack copy];
}

+ (NSString *)descriptionOfRegion:(id)region
{
    return @"This value";
}

@end

Upvotes: 1

Views: 1034

Answers (1)

Jim Puls
Jim Puls

Reputation: 81072

Your table view controller needs to implement at least

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

and possibly

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

as well, so the table view knows how many cells to ask for.

Upvotes: 2

Related Questions