开发者

Help me ...Terminating app due to uncaught exception 'NSRangeException',

- (void)viewDidLoad {
    [super viewDidLoad];
    NSOperationQueue *queu开发者_C百科e = [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


does the array has any data when you show the table? if you give a default number of cells that is bigger than 0 and there is no data in the array..then you will receive the error you described.

'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'

EDIT: in

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [appDelegate.ArrParseData count];
}

you shouldn't return the number of elements from the appDelegate.ArrParseData because if that array is filled with data then you will receive a number X bigger than 0.

in the cellForRowAtIndexPath you ask for information at index X but self.array is not filled yet with data because you call the loadImage method async.

You should return [self.array count]; in the numberOfRowsInSection method.


I don't think you are showing all the code you need to show - the trace shows the exception happens some time in cellForRowAtIndexPath. Looks like you are accessing an array that contains nothing - are you completely sure the array has been loaded by the time you try to use it?

Likely you know how many cells there will be and you report that correctly through numberOfRowsInSection, but you haven't populated the array by the time it is required. You need to so something like display placeholders until the load is finished, leave the images nil (empty) until ready, make the data load/view appearance sequential, get the load happening in the background earlier or something like that.


Your number of rows method is accessing a different count array from the one which contains your image, isn't it?

If you executed this code in no concurrent way, you wouldn't have this error. The problem comes when the thread who has to fill in your images array is slower than the main one.

So your best solution would be to check the images array size before accessing it and if you want you could send a reloadRowsAtIndexPaths: withRowAnimation: message to your tableView every time that you get a new image. This would give you a nice loading effect.

I hope I've been clear enough.

See you!


maybe someone make such stupid mistake like me in: - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath and try to do something like this:SomeObject = [SomeContainer objectAtIndex:indexPath]; Or even try to be clever with cast: SomeObject = [SomeContainer objectAtIndex:(int)indexPath]; WRONG

no way...JUST remember to get index you have to call indexPath.row!!!!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜