- (void)viewDidLoad {
[super viewDidLoad];
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self selector:@selector(loadImage) object:nil];
[queue addOperation:operation];
[operation release];
NSMutableArray *_array = [[NSMutableArray alloc] initWithCapacity:10000];
self.array = _array;
[_array release];
}
- (void)loadImage
{
for(int i = 0;i < [appDelegate.ArrParseData count]; i++ )
{
NSLog(@" count %i",i);
// Configure the cell...
XMLTags *xmltag = [appDelegate.ArrParseData objectAtIndex:i];
NSString *Img_id, *Img_name, *DynamicImgUrl;
Img_id = xmltag.equip_id;
Img_name = xmltag.image;
DynamicImgUrl = [NSString stringWithFormat:@"http://testetete.com/pics/equipment/%@/%@",Img_id, Img_name];
NSURL *ImageUrl = [NSURL URLWithString:DynamicImgUrl];
NSLog(@" ccxvount %@",ImageUrl);
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:ImageUrl]];
NSLog(@" ccxvount %i",i);
[self.array addObject:image];
NSLog(@" ccxvount %i",i);
}
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIImageView *top_img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 25, 75, 80)];
CGRect rect = top_img.frame;
rect.size.height = 60;
rect.size.width = 60;
top_img.frame = rect;
// the imageView holds the image myImg
top_img.image = [self.array objectAtIndex:indexPath.row];
//cell.imageView.image=image;
[cell.contentView addSubview:top_img];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [appDelegate.ArrParseData count];
}
when i excecute the above code my application gets crashes with the following error..
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
0 CoreFoundation 0x026cc919 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0281a5de objc_exception_throw + 47
2 CoreFoundation 0x026c2465 -[__NSArrayM objectAtIndex:] + 261
3 AgSearch 0x00006f80 -[EquipmentViewController tableView:cellForRowAtIndexPath:] + 752
4 UIKit 0x00345a3f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 619
5 UIKit 0x0033bad2 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
6 UIKit 0x0035040c -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
7 UIKit 0x003484bc -[UITableView layoutSubviews] + 242
8 QuartzCore 0x024620d5 -[CALayer layoutSublayers] + 177
9 QuartzCore 0x02461e05 CALayerLayoutIfNeeded + 220
10 QuartzCore 0x0246164c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 302
11 QuartzCore 0x024612b0 _ZN2CA11Transaction6commitEv + 292
12 QuartzCore 0x02468f5b _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
13 CoreFoundation 0x026add1b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
14 CoreFoundation 0x02642987 __CFRunLoopDoObservers + 295
15 CoreFoundation 0x0260bc17 __CFRunLoopRun + 1575
16 CoreFoundation 0x0260b280 CFRunLoopRunSpecific + 208
17 CoreFoundation 0x0260b1a1 CFRunLoopRunInMode + 97
18 GraphicsServices 0x02f312c8 GSEventRunModal + 217
19 GraphicsServices 0x02f3138d GSEventRun + 115
20 UIKit 0x002e3b58 UIApplicationMain + 1160
21 AgSearch 0x0000206c main + 102
22 AgSearch 0x00001ffd start + 53
23 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Any help would be greatly appreciated....
Thanks for nay help
numberOfSectionsInTableView:
method.NSMutableArray *_array = [[NSMutableArray alloc] initWithCapacity:10000];
does not create an array with 10000 entries. It creates an array with 0 entries. – Matthias Bauchqueue
NSOperationQueue. If you want to hold on to it, make it an instance variable and release it later when you're done with it. – Brad Larson