-3

I am creating an application for IOS and am struggling with accessing an object inside an array i have made for display on the table view cell.

This is the code i have used to add the object to the array every time the loop cycles through.

for (int i = 0; i < [parsedArray count]; i++) {

    HPTBusStops *busStops = [[HPTBusStops alloc] init];
    NSArray *informationArray = [parsedArray objectAtIndex:i];

    busStops.busStopNumber = [informationArray objectAtIndex:0];
    busStops.busStopName = [informationArray objectAtIndex:1];
    busStops.latitude = [informationArray objectAtIndex:2];
    busStops.longitude = [informationArray objectAtIndex:3];

    [self.busStopsHolder addObject:busStops];

}

The HPTBusStops class is obviously custom, and later in the master view controller, i am tring to re-access these properties through the busStopsHolder array, when programming the cell, in this part:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HPTBusStopsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BusStopCell" forIndexPath:indexPath];

    return cell; 
}

I am honestly very unsure of how to access the busStops object's properties through the busStopHolder's array.

Any help would be appreciated

Thanks Hamish

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • Your question makes no sense. You're accessing elements of the `parsedArray` and the `informationArray`, but you don't know how to access elements of the `busStopsHolder` array??? – Hot Licks Aug 23 '14 at 02:24
  • I suspect your question is really a dupe of this: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Hot Licks Aug 23 '14 at 03:06

2 Answers2

0

An oversimplified way to do this is as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HPTBusStopsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BusStopCell" forIndexPath:indexPath];

    HPTBusStops *busStops = yourSourceArray[indexPath.row];
    cell.textLabel.text = busStops.name;

    return cell;
}

If your tableView has one section, you can use the tableView's indexPath.row property to determine which index you're dealing with, use that as the index to access the relevant object in your source array, and then customize the cell based on the specific object at that index. In the example code I just assumed there was a name property on the busStops object for example purposes.

Make sure in your numberOfRowsInSection method you set the number of rows as the number of objects in your source array, whatever that is.

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return yourSourceArray.count;
}
Mike
  • 9,765
  • 5
  • 34
  • 59
0

It looks to me like you might have an issue with scope...

You'll need to make "busStopsHolder" a @property of the class containing the for loop and instantiate that class as an instance variable in your Master View Controller.

in your class, say MyInfoClass.h:

@property (strong, nonatomic)  NSMutableArray *busStopsHolder;

in MyInfoClass.m:

synthesize busStopsHolder;

Make sure you have initialized your busStopsHolder array in MyInfoClass...

(id) init {

    busStopsHolder = [[NSMutableArray alloc] init];

}

Then in your master view controller .h:

#import "MyInfoClass.h"

@interface myMastserViewController : UIViewController  {

    MyInfoClass *infoClass;

}

Then...

- (void) viewDidLoad {

    infoClass = [[MyInfoClass alloc] init];

    [infoClass methodToLoadBusStops];

}

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

    HPTBusStopsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BusStopCell" forIndexPath:indexPath];

    HPTBusStop *busStop = [[infoClass busStopsHolder] objectAtIndex:indexPath.row];

    [[cell textLabel] setText:[busStop busStopName]]

    return cell; 
}
BradleyMorgan
  • 138
  • 1
  • 6
  • Your logic omits creating the `busStopsHolder` array. Failing to create the array is the most common cause of problems in situations like this. – Hot Licks Aug 23 '14 at 02:25
  • True. I thought that would result in a segfault, though...so, since the OP did not mention an error, I assumed that the array had been initialized at some point in the code. Thanks for the feedback--post edited to show the init of busStopsHolder. – BradleyMorgan Aug 23 '14 at 02:31
  • 1
    No, failing to create the object **does not result in a segfault**. Rather, operations on the "object" (nil pointer) silently fail, returning zero/nil values. This problem can cascade through a half-dozen layers before something trips on it and causes a noticeable error. – Hot Licks Aug 23 '14 at 02:38