0

私のプロジェクトでは、画像を携帯電話のドキュメント フォルダーに保存してから、別のテーブルビューに読み込んでいます。少し成功しています。最後に取得した画像がテーブルに読み込まれますが、最後の画像だけでなくすべての行に読み込まれます。テーブルビューに画像をロードするために使用したコードは次のとおりです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"List";
ListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//Load PLIST
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [path objectAtIndex:0];
NSString *plistPath = [NSString stringWithFormat:@"%@/images.plist", documentsDirectory];
//Load PLIST into mutable array
NSMutableArray *imageArray = [NSMutableArray arrayWithContentsOfFile:plistPath];

for (NSDictionary *dict in imageArray) {
//Do whatever you want here, to load an image for example
   NSString *imageFilePath = [NSHomeDirectory() stringByAppendingPathComponent:[dict objectForKey:@"Original Image"]];
   UIImage *image = [UIImage imageWithContentsOfFile:imageFilePath];
   [cell.imageofItem setImage:image];
}   
}

何が起こっているかの例を次に示します。2 枚の写真を撮るとします。次に、セルに画像をロードすると、最後の写真が 2 つのセルにロードされます。

どんな助けでも大歓迎です!

4

1 に答える 1

1

それ以外の

for (NSDictionary *dict in imageArray) {
   NSString *imageFilePath = [NSHomeDirectory() stringByAppendingPathComponent:[dict objectForKey:@"Original Image"]];
   UIImage *image = [UIImage imageWithContentsOfFile:imageFilePath];
   [cell.imageofItem setImage:image];

}

試す

   NSDictionary *dict = [imageArray objectAtIndex:indexPath.row];
   NSString *imageFilePath = [NSHomeDirectory() stringByAppendingPathComponent:[dict objectForKey:@"Original Image"]];
   UIImage *image = [UIImage imageWithContentsOfFile:imageFilePath];
   [cell.imageofItem setImage:image];

問題は、indexPath.row ごとに、配列の最後の要素まで反復し、最後の画像に到達するまでセル画像を常に上書きしていたことです。次に、次の indexPath.row に対して同じことを行い、それを配列内の最後の画像に設定します。

于 2012-10-28T03:13:32.953 に答える