1

i have 60 rows in an table view.i have an array named as "BundleImagesArray " with 60 bundle images names.So i am retrieving the images from bundle and creating thumbnail to it. whenever binding each cell at first time ,i am storing the Thumbnail images in to an array.because of enabling the fast scrolling after bind each cell.i am utilizing the images from the array(without creating the thumbnail again).however the imageCollection array(which will store thumbnail images) is disorder some times

the Index path.row is coming as 1,2.....33,34,50,51..etc

its not an sequential order.so i am getting trouble with my imageCollection array which is used to store and retrieve images according to the index path.may i know what is the reason for that.can any one provide me a good solution for this.is there any way to get the indexpath.row as sequential order?

my cellForRowAtIndexPath code is:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {    

     NSLog(@"IndexPath Row%d",indexPath.row);
     static NSString *CellIdentifier = @"Cell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil)
     {

      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
     }    


     if ([cell.contentView subviews])
     {
      for (UIView *subview in [cell.contentView subviews])
      {
       [subview removeFromSuperview];
      }
     }

     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     [cell setAccessoryType:UITableViewCellAccessoryNone];

     Class *temp = [BundleImagesArray objectAtIndex:indexPath.row];

     UIImageView *importMediaSaveImage=[[[UIImageView alloc] init] autorelease];
     importMediaSaveImage.frame=CGRectMake(0, 0, 200,135 );
     importMediaSaveImage.tag=indexPath.row+1;
     [cell.contentView addSubview:importMediaSaveImage]; 
     UILabel *sceneLabel=[[[UILabel alloc] initWithFrame:CGRectMake(220,0,200,135)] autorelease];

     sceneLabel.font = [UIFont boldSystemFontOfSize:16.0];
     sceneLabel.textColor=[UIColor blackColor];
     [cell.contentView addSubview:sceneLabel];


    //for fast scrolling
       if([imageCollectionArrays count] >indexPath.row){


        importMediaSaveImage.image =[imageCollectionArrays ObjectAtIndex:indexPath,row];
     }
     else {

//for start activity indicator 
      [NSThread detachNewThreadSelector:@selector(showallstartActivityBundle) toTarget:self withObject:nil];
      NSData *datas = [self photolibImageThumbNailData:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:temp.fileName ofType:@"png" inDirectory:@"Images"]]];

      importMediaSaveImage.image =[UIImage imageWithData:datas]; 

      [imageCollectionArrays addObject:importMediaSaveImage.image];



//to stop activity indicator
      [NSThread detachNewThreadSelector:@selector(showallstopActivityBundle) toTarget:self withObject:nil];
     }





     sceneLabel.text = temp.sceneLabel;

     temp = nil;
     return cell;
    }
3
  • Could you show how you fill your imageCollectionArrays array? Commented Aug 20, 2011 at 22:37
  • The order is dependent on what is displayed and how you scroll and determined by the tableView. The imageCollectionArrays should never be out of sync with the BundleImagesArray, and if that is the case, the value of indexPath.row should not matter, as long as it is in the range 0-59. Commented Aug 20, 2011 at 22:40
  • What is the trouble you are having with the imageCollection array? That is solution to look for. Commented Aug 21, 2011 at 2:42

2 Answers 2

1

Getting the tableview to call you in a particular order is not the right solution. You need to be able to provide any cell it needs in any order.

I think the problem is that you are using an array and trying to access indexes that don't exist yet. You could use an NSMutableDictionary for your imageCollectionArrays (instead of NSArray) and store your data there, keyed by row (or NSIndexPath). They you can add or retrieve them in any order.

Sign up to request clarification or add additional context in comments.

Comments

0

An NSArray is perfectly fine for UITableView rows. The rows are ordered, and an array is also ordered, so no problem there.

The problem in your code seems to be that you are using two arrays, one immutable and one mutable. You are adding objects to the mutable array, and so it is not exactly predictable where they will be added (because it is not predictable that the rows are going to be needed in order).

I recommend filling your NSMutableArray first with [NSNull null] objects, and then inserting the image at a specific point in the array:

// during initialization 
for (int i=0; i<numberOfImages; i++) {
    [imageCollectionArrays addObject:[NSNull null]];
}

// in cellForRowAtIndexPath:
[imageCollectionArrays replaceObjectAtIndex:indexPath.row 
    withObject:importMediaSaveImage.image];

Try that, see if it works.

5 Comments

NSArray would be fine if it was filled in order, but if you use lazy loading and load images into the array in the order that the cells are requested you have a problem - you can't insert at index N if you haven't already put something in 0 .. N-1
@progrmr: Well, with replaceObjectAtIndex:withObject: you can do exactly that.
@Mundi: If I have an NSMutableArray with 2 objects in it, you cannot insert or replace an object except at index 0, 1 or 2. If you attempt to replaceObjectAtIndex 5 or any index > 2 you will get an exception thrown. doc ref "The index of the object to be replaced. This value must not exceed the bounds of the array."
Exactly right, that's why you fill it with nulls first... ;-). But you are right: the correct thing to do is check the bounds first.
You mean fill it with [NSValue valueWithPointer:nil] objects? 'cause you can't put a nil or NULL into an NSArray directly. Then you have to test for NSValue objects when extracting elements, it just seems ugly to me, which is why I recommend an NSMutableDictionary or malloc a real array (which you can fill with NULL). That's my preference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.