Hey.
Recently edited to adapt to some answers and to improve readability
I am loading in data to a UITableView
, from a custom UITableViewCell
(own class and nib). It works great, until I try to access the objectAtIndex:indexPath.row
for some arrays.
I'll post my code first, it will probably be easier for you to understand what I mean then.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[CustomCell class]]){
cell = (CustomCell *) currentObject;
break;
}
}
}
// Configure the cell...
NSUInteger row = indexPath.row;
cell.titleLabel.text = [postsArrayTitle objectAtIndex:indexPath.row];
cell.dateLabel.text = [postsArrayDate objectAtIndex:indexPath.row];
cell.cellImage.image = [UIImage imageWithContentsOfFile:[postsArrayImgSrc objectAtIndex:indexPath.row]];
NSLog(@"%d", indexPath.row);
return cell;
}
The odd thing is, it does work when it loads in the first three cells (they are 130px high), but crashes when I try to scroll down. (Aka, the log shows three numbers before it crashes, 0, 1, 2
)
So, as a summary, the objectAtIndex:indexPath.row
gets run 3*3 times successfully, but when I try to scroll down in the app, loading in new cells, the app crashes with the following error:
2010-05-30 14:00:43.122 loveapple.no[2582:207] -[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x5a44bf0
2010-05-30 14:00:43.124 loveapple.no[2582:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x5a44bf0'
[...]
0 CoreFoundation 0x02398c99 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x024e65de objc_exception_throw + 47
2 CoreFoundation 0x0239a7ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x0230a496 ___forwarding___ + 966
4 CoreFoundation 0x0230a052 _CF_forwarding_prep_0 + 50
5 myappname 0x00002ab1 -[HomeTableViewController tableView:cellForRowAtIndexPath:] + 605
[...]
ADDITIONAL INFORMATION ON THE ARRAYS:
The arrays are created in the .h
file:
NSArray *postsArrayDate;
NSArray *postsArrayTitle;
NSArray *postsArrayComments;
NSArray *postsArrayImgSrc;
And filled in the viewDidLoad:
:
NSURL *urlPosts = [NSURL URLWithString:@"http://mysite/myphpfile.php?posts=2"]; //returns data in this format: DATA1#Data1.1#data1.2~DATA2#data2.1#~ and so on.
NSError *lookupError = nil;
NSString *data = [[NSString alloc] initWithContentsOfURL:urlPosts encoding:NSUTF8StringEncoding error:&lookupError];
postsData = [data componentsSeparatedByString:@"~"];
[data release], data = nil;
urlPosts = nil;
postsArrayDate = [[postsData objectAtIndex:2] componentsSeparatedByString:@"#"];
postsArrayTitle = [[postsData objectAtIndex:3] componentsSeparatedByString:@"#"];
postsArrayComments = [[postsData objectAtIndex:4] componentsSeparatedByString:@"#"];
postsArrayImgSrc = [[postsData objectAtIndex:5] componentsSeparatedByString:@"#"];
Thanks for any help.