简体   繁体   English

IndexPath.row问题

[英]Problem with IndexPath.row

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. 我在表view中有60行。我有一个名为“ BundleImagesArray”的数组,其中包含60个包图像名称。因此,我要从包中检索图像并为其创建缩略图。 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 每当第一次绑定每个单元格时,我都会将Thumbnail图像存储到一个数组中。由于在绑定每个单元格后启用了快速滚动。我正在利用数组中的图像(而无需再次创建缩略图)。但是imageCollection数组(它将存储缩略图)有时会混乱

the Index path.row is coming as 1,2.....33,34,50,51..etc 索引path.row变为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? 它不是顺序的。所以我在使用我的imageCollection数组时遇到了麻烦,该数组用于根据索引路径存储和检索图像。我可能知道这是什么原因。任何人都可以为此提供一个很好的解决方案。有什么方法可以按顺序获取indexpath.row?

my cellForRowAtIndexPath code is: 我的cellForRowAtIndexPath代码是:

 - (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;
    }

Getting the tableview to call you in a particular order is not the right solution. 让tableview按特定顺序打电话给您不是正确的解决方案。 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). 您可以为您的imageCollectionArrays(而不是NSArray)使用NSMutableDictionary并将数据存储在此处,并按行(或NSIndexPath)进行键控。 They you can add or retrieve them in any order. 您可以按任何顺序添加或检索它们。

An NSArray is perfectly fine for UITableView rows. NSArray非常适合UITableView行。 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: 我建议先用[NSNull null]对象填充NSMutableArray ,然后将图像插入数组中的特定点:

// 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. 尝试一下,看看是否可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM