0

テーブル ビューといくつかのカスタム セルに問題があります。これが私のコードです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForRowAtIndexPath");
static NSString *MyIdentifier = @"customCell";

EventTableCell *cell = (EventTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {
    //cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    [[NSBundle mainBundle] loadNibNamed:@"EventTableCell" owner:self options:nil];
    cell = self.eventCell;
}

//Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
[[cell eventNameLabel] setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
[[cell eventDateLabel] setText:[[stories objectAtIndex: storyIndex] objectForKey: @"date"]];

NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"link"];

// clean up the link - get rid of spaces, returns, and tabs...
storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:@"  " withString:@""];

//NSString *string = [[NSString alloc] stringWithString:[[stories objectAtIndex: storyIndex] objectForKey: @"link"]];
NSURL *url = [NSURL URLWithString:storyLink];
NSData *data = [[NSData alloc] initWithContentsOfURL: url];
UIImage *image = [UIImage imageWithData: data];
[[cell eventImage] setImage:image];

return cell;
}

私のテーブルには、これらのカスタム セルが多数あり、ビューが一度に表示できる数を超えています。したがって、すべてのセルを表示するには、上下にスクロールする必要があります。私の問題はこれです。スクロールすると大幅に遅れ、デバッガーコンソールでは、新しいセルが表示されるたびに cellForRowAtIndexPath が呼び出されます。各セルには、URL からダウンロードされた画像が含まれているため、UIImage に変換する必要があります。これが遅れの原因だと思います。

画像をダウンロードし、アプリで大きな遅延を引き起こすことなくセルに表示するために何をする必要があるかについて、誰かが私に指示できますか?

IE: セルを表示する前に画像が画像として保存されるように、ダウンロード/変換コードをどこに配置しますか?

ありがとう、

ジャック

4

1 に答える 1

2

画像を事前にダウンロードしたい場合は、アプリケーションの任意の時点で実行し、後で使用できるようにキャッシュすることができます。たとえば、RSSフィードをダウンロードして解析した直後にダウンロードを開始できます。によって返されるディレクトリにそれらを保存できます[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]。それらをダウンロードするには非同期方式を使用するのが最善であり、最高のパフォーマンスを得るには、一度に数回のダウンロードをアクティブにするのが最善です。

必要に応じてロードする場合は、セルを割り当てるときにプレースホルダーイメージを設定し、非同期のNSURLConnectionなどを使用してダウンロードします。ダウンロードが完了したら、プレースホルダーを実際の画像に置き換えます(もちろん、後で再利用するためにキャッシュします)。

于 2011-05-28T16:50:49.723 に答える