0

私のアプリケーションでは、UITableView に複数の画像を表示する必要があるため、大きな画像を UITableViewCells にロードする適切な方法について Web ですでに多くのことを検索しました。より明確にするために、アプリが実行する手順を分割します。

  1. 画像を非同期でダウンロードします。
  2. それらを NSHomeDirectory(); に保存します。

=> Thins 部分は完全に機能しています。

問題は、UITableViewCell に画像を表示する方法です。すでに UIImageView をセルの contentView に追加しようとしましたが、スクロールのパフォーマンスに少し影響がありました。Apple のガイドを検索しましたが、正しい方法は UIImageView を追加することだと思いますセルに移動し、NSHomeDirectory() から画像をロードするので、次のようになります。

UITableViewCell をカスタマイズし、UIImageView の (302x302px) を追加する最良の方法は何ですか?

4

3 に答える 3

2

同じ質問があります。私は次のことをしています:

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

    CustomCell* cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier];
    }

    // add subview to cell
    if (cell.customView == NULL) {
         cell.customView = [[CustomView alloc] initWithFrame:cell.frame];
         [cell.contentView addSubview:cell.customView];
    }

    // bind cell data

    return cell;
}
于 2013-05-24T01:08:30.067 に答える
2

最高のスクロール パフォーマンスを得るには、UITableViewCell のコンテンツを自分で描画する必要があります。Tweetie アプリ (現在は公式 Twitter アプリ) の作成者である Loren Brichter は、これについて非常に有名なブログ記事を書いています。残念ながら、このブログ記事は削除されました。 ただし、この記事が役立つ場合があります。高速スクロールについて説明し、例を示し、Loren Brichter によるプレゼンテーションのビデオがあります。

基本的に、やりたいことは、UITableViewCell をサブクラス化し、drawRect:メソッドをオーバーライドすることです。画像を表示するには、次のようにします。

- (void)drawRect:(CGRect)rect
{
    [myImage drawAtPoint:CGPointMake(10, 10)];
}

このようにして、多くのサブビューをレイアウトすることを避けます。

于 2012-10-14T20:31:54.653 に答える
0

まず、UITableViewのカスタムセルを作成し、次のポイントを実行し続ける必要があります。

  1. 各行の高さを302ピクセルに設定します

    -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    

    {302.0を返す; }

  2. 次のコードを使用して、テーブルの各セルにUIImageViewを作成します-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@ "cell"];

            const NSInteger IMAGE_VIEW_TAG=1001;
    
            UIImageView *imageView;
    
            if(cell==nil)
            {
                    cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];
    
                    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    
                    imageView =[[[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 302, 302)] autorelease];
                    imageView.tag=IMAGE_VIEW_TAG;
                    imageView.contentMode=UIViewContentModeScaleAspectFit;
                    [cell.contentView addSubview:imageView];
             }
    
    imageView=(UIImageView*)[cell.contentView viewWithTag:IMAGE_VIEW_TAG];
    [imageView setImage:[UIImage imageNamed:@"image.png"];
    return cell;
    

    }

  3. 表示する行数を設定する

     -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     {
              return 5;
     }
    
  4. TableViewデリゲートとデータソース、UITableViewDelegateとUITableViewDataSourceを追加することを忘れないでください

于 2012-10-15T11:55:49.417 に答える