1

テーブルのセルに画像を表示しています。私は中にコードを持っています

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

ストーリーボードを使用しているときに、セルに画像を表示しました

cell.imageView.image = [UIImage imageNamed:_lotImagesArray[row]];

ただし、Webサーバーから画像を読み込もうとすると、画像がセルのラベルの上に表示されます(ラベルテキストがブロックされます)

Webサーバーからの画像を表示するために使用したコードは次のとおりです。

NSString *strURL = [NSString stringWithFormat:@"http://www.domainhere.com/images/%@", lotPhoto[row]];
NSURL *url = [[NSURL alloc] initWithString:strURL ];
cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

誰かが私がどこで失敗したのかアドバイスしてもらえますか?

4

1 に答える 1

1

可能性のあるいくつかの可能性:

まず、あなたが持っていることを確認するために再確認したいかもしれません

cell.imageView.clipsToBounds = YES;

そうしないと、画像がに収まるようにサイズが変更されるUIImageViewと、画像が画像ビューの境界を越えてにじむ可能性があります。特に、この画像をバックグラウンドキューで読み込んでいるときに、この問題に気づきました。

次にimageView、バックグラウンドでのプロパティを設定している場合UITableViewCell(以下の簡略化されたサンプルコードのように)、バックグラウンド画像の読み込みプロセスを開始する前に、空白の画像で画像を適切に初期化することが重要であることを知っておく必要があります。したがって、Webベースのソースからセルをロードするときの非常に一般的なコードの例は次のようになります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.textLabel.text = ... // you configure your cell however you want

    // make sure you do this next line to configure the image view

    cell.imageView.image = [UIImage imageNamed:@"blankthumbnail.png"];

    // now let's go to the web to get the image

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

        UIImage *image = ... // do the time consuming process to download the image

        // if we successfully got an image, remember, ALWAYS update the UI in the main queue
        dispatch_async(dispatch_get_main_queue(), ^{
            // let's make sure the cell is still visible (i.e. hasn't scrolled off the screen)
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            if (cell)
            {
                cell.imageView.image = image;
                cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
                cell.imageView.clipsToBounds = YES;
            }
        });
    });

    return cell;
}

明らかに、そのようなことをする場合は、[UIImage imageNamed:@"blankthumbnail.png"];が戻ってこないことを確認nilする必要があります(つまり、アプリがバンドル内で正常に検出されていることを確認してください)。一般的な問題には、空白のイメージがまったくない、名前が間違っている、[ビルドフェーズ]タブの[バンドルリソースのコピー]の下にあるターゲット設定にイメージが含まれていないなどがあります。

第3に、サブクラス化さUITableViewCellれたエントリを使用する場合は、、などの標準プロパティ名を使用しないようにする必要UITableViewCellがあります。必ず独自の一意の名前を使用してください。を使用すると、システムはの新しいプロパティとデフォルトのプロパティの間で混乱します。imageViewtextLabelimageViewIBOutletimageViewUITableViewCell

于 2012-11-28T00:06:08.500 に答える