簡體   English   中英

IndexPath.row問題

[英]Problem with IndexPath.row

我在表view中有60行。我有一個名為“ BundleImagesArray”的數組,其中包含60個包圖像名稱。因此,我要從包中檢索圖像並為其創建縮略圖。 每當第一次綁定每個單元格時,我都會將Thumbnail圖像存儲到一個數組中。由於在綁定每個單元格后啟用了快速滾動。我正在利用數組中的圖像(而無需再次創建縮略圖)。但是imageCollection數組(它將存儲縮略圖)有時會混亂

索引path.row變為1,2 ..... 33,34,50,51..etc

它不是順序的。所以我在使用我的imageCollection數組時遇到了麻煩,該數組用於根據索引路徑存儲和檢索圖像。我可能知道這是什么原因。任何人都可以為此提供一個很好的解決方案。有什么方法可以按順序獲取indexpath.row?

我的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;
    }

讓tableview按特定順序打電話給您不是正確的解決方案。 您需要能夠以任何順序提供所需的任何單元。

我認為問題在於您正在使用數組並嘗試訪問尚不存在的索引。 您可以為您的imageCollectionArrays(而不是NSArray)使用NSMutableDictionary並將數據存儲在此處,並按行(或NSIndexPath)進行鍵控。 您可以按任何順序添加或檢索它們。

NSArray非常適合UITableView行。 行是有序的,數組也是有序的,所以在那里沒有問題。

您的代碼中的問題似乎是您正在使用兩個數組,一個不可變,一個可變。 您正在將對象添加到可變數組,因此無法准確預測將添加對象的位置(因為無法預測需要按順序排列行)。

我建議先用[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];

嘗試一下,看看是否可行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM