'Objective C iPhone. Getting NSMutableArray in Table [closed]

I try to get the data of an array in an TableView but get an bad access error all the time. My code looks like this:

NewsViewController.h

#import "StatusMessage.h"

@interface NewsViewController : UIViewController {
    IBOutlet UITableView *table;
    NSMutableArray *statusMessages; 
}

@property(nonatomic, retain) NSMutableArray *statusMessages;

@end

NewsViewController.m

#import "NewsViewController.h"
#import "TBXML.h"

@implementation NewsViewController

@synthesize statusMessages;

-(void) getXML {
    //some XML things parsing above

    for (int i = 0; i smaller [allMessages count]; i++) {
        statusMessage *temp = [[StatusMessage alloc]init];

        NSLog(@"retain count %d", [statusMessages retainCount]); //is 1
        [temp initstatusMessage: str1:str2:str3:str4];

        [statusMessages addObject: temp];
        NSLog(@"retain count %d", [statusMessages retainCount]); // is 1
        [temp release];
    }   
}

-(void) getLda {    
    self.getXML;
    NSLog(@"retain count %d", [statusMessages retainCount]); // is 1

    NSComparisonResult dateSort(StatusMessage *d1, StatusMessage *d2, void *context) {
        return [[d2 getDate] compare:[d1 getDate]];
    }       

    [statusMessages sortUsingFunction:dateSort context:nil];
}

- (void)loadView {
    [[self navigationController] setNavigationBarHidden:NO animated:NO];
    table.backgroundColor = [UIColor clearColor];   
    [super loadView];
}



- (void)viewDidLoad {   
    [super viewDidLoad];

    statusMessages = [[NSMutableArray alloc] initWithObjects:nil];  
    NSLog(@"retain count %d", [statusMessages retainCount]); // is 1    

    self.getLda;
    NSLog([[statusMessages objectAtIndex:1] getText]); //works fine for every index
}

- (void)dealloc {
    [statusMessages release];   
    [super dealloc];    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [statusMessages count];
}


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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"retain count %d", [statusMessages retainCount]); // is 1 every time

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [[statusMessages objectAtIndex:indexPath.section] getText];
    cell.textLabel.font = [UIFont systemFontOfSize:15];

    return cell;    
}

NewsMessage.h

#import 


@interface statusMessage : NSObject {
    NSString *source;
    NSString *text;
    NSString *date;
    NSURL    *link; 
}

-(void) initstatusMessage: (NSString *) s: (NSString *) t: (NSString *) l:(NSString *) d;
-(NSString *) getText;
-(NSDate   *) getDate;

@end

NewsMessage.m

#import "statusMessage.h"


@implementation statusMessage

-(void) initstatusMessage: (NSString *) s: (NSString *) t: (NSString *) l:(NSString *) d {
    source = s;
    link   = [[NSURL alloc] initWithString:l];

    text = t;

    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"yyyy-MM-ddeHH:mm:ssZZZZ"];
    date = [formatter dateFromString: d]; 
}

-(NSString *) getText {
    return (NSString *) CFURLCreateStringByReplacingPercentEscapesUsingEncoding (kCFAllocatorDefault , text, CFSTR(""), kCFStringEncodingUTF8);
}

-(NSDate *) getDate {
    return date;
}

@end

I just can't access the data in the cellForRowAtIndexPath-Method.



Solution 1:[1]

setting up a tableview is not just the method 'cellForRowAtIndexPath'. there are some other methods you have to implement. e.g.

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

and some more. it depends also on what you want your tableview is able to. you should make a tableview-tutorial or look in a tutorial project file where you find all the important things you have to do. (google for 'uitableview tutorial', there is so much out in the web...)

Solution 2:[2]

You are also over-retaining your array, you do:

[[NSMutableArray alloc] initWithObjects:nil] retain];

when you really just need:

[[NSMutableArray alloc] initWithObjects:nil];

Since alloc/init leaves the retain count at one as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Micko
Solution 2 Kendall Helmstetter Gelner