I’m writing an app that pulls a .txt file from the web, parses it into a 2 dimensional array (essentially. It’s actually an NSMutableArray with an NSArray for each element) and then displays each NSArray inside of a UITableView (it loads each NSArray by using sublabels).
Anyways, once I scroll down to the 17th cell (index of 16) the app crashes…
The UITableView should have 483 cells (for testing purposes), but I can’t seem to scroll past the 16th cell without it crashing.
Edit to post code and error:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[[tableView layer] setCornerRadius:3.0];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"]autorelease];
}
NSInteger artistIndex = 1;
NSInteger albumIndex = 3;
NSInteger dateIndex = 6;
NSInteger imageIndex = 5;
// ARTIST
CGRect frame = CGRectMake(59, 11, 244, 13);
UILabel *label = [[UILabel alloc]initWithFrame:frame];
label.font = [UIFont boldSystemFontOfSize:13];
label.textColor = [UIColor blackColor];
label.text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:artistIndex];
[cell addSubview:label];
// ALBUM (more like description...
frame = CGRectMake(60, 30, 244, 11);
label = [[UILabel alloc]initWithFrame:frame];
label.font = [UIFont boldSystemFontOfSize:11];
label.textColor = [UIColor darkGrayColor];
label.text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:albumIndex];
[cell addSubview:label];
// DATE
frame = CGRectMake(59, 49, 244, 10);
label = [[UILabel alloc]initWithFrame:frame];
label.font = [UIFont fontWithName:@"Helvetica" size:10.0];
label.textColor = [UIColor darkGrayColor];
label.textAlignment = UITextAlignmentRight;
label.text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:dateIndex];
[cell addSubview:label];
// IMAGE
NSString *urlString = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:imageIndex];
NSData *urlData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]];
UIImage *myImage = [[UIImage alloc]initWithData:urlData];
UIImageView *myImageView = [[UIImageView alloc] init];
myImageView.image = myImage;
myImageView.frame = CGRectMake(8,9,44,44);
[myImageView.layer setMasksToBounds:YES];
[myImageView.layer setCornerRadius:3.0];
[[cell contentView] addSubview:myImageView];
[urlData release];
[myImage release];
[myImageView release];
[label release];
return cell;
NOTE: I feel like this code is pretty awful… If you guys have any suggestions to clean it up, I’d love to hear it.
And here’s the error message:
* Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘* -[NSMutableArray objectAtIndex:]: index 6 beyond bounds [0 .. 3]’
My guess is that you have this, and the others like it, backwards.
Try,
In my experience, dealing with two-dimensional arrays in objective-c can be sometimes confusing. Especially when working with
UITableViews.What makes me have a feeling I am right is this,
1 + 3 + 6 + 5 = 15, and one more, at 16, is the precise index that you are crashing at.