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.
3. (and hopefully final) UPDATE:
When you pass an argument to a function, the argument does not get retained. Same thing is true for assigning variables. In other words:
source = s; text = t; date = [formatter dateFromString: d];
are not retained by your class (link... however is, since you use "alloc"). So, use
source = [s retain];
text = [t retain];
date = [[formatter dateFromString: d]retain];
and you should be fine. Don't forget to release them in your dealloc.
Please take a close look at the concept of memory management again.