3

複数のサブビューを含むカスタム UITableCellView を構築しようとしていますが、現在、そのうちの 2 つ (最大のもの) のみに苦労しています: UIImageView (画像はから来ているので、代わりに UIWebView を使用してもかまいません)インターネット) と、スクロールせずにすべてのテキストを一度に表示する必要がある UITextView。このシナリオでは、XIB ファイルとサブクラス化 UITableCellView (Autolayout disable) を使用してカスタム セルの構築を開始しました。

ここに画像の説明を入力

カスタム クラスに 2 つの新しいプロパティ (IBOutlet) を追加しました (非常に単純です)。

@interface NewsFeedPostCell : UITableViewCell<UITextViewDelegate>
{
    UIImageView *picView;
    UITextView *postContent;
}

@property IBOutlet UIImageView *picView;
@property IBOutlet UITextView *postContent;

@end

その後、それらを使用してデータを入力しています(iTunesから美しいJSONを返すものを取得しています@" http://itunes.apple.com/search?term=ice%20age&country=us&entity=movie " )。現時点では、各セルの正確なサイズを取得せずにデータを適切に表示することに焦点を当てていますが (異なるテキストのサイズは別として)、多くの問題があります: テキストがすべて表示されていないため、コンテンツが正しく表示されません。行に画像が表示されないなど。私はここstackoverflowで多くのことを読んでいますが、コードのみを使用してそれを取得することもテストしており、同様のことがあったため、人々がそれを解決するさまざまな方法について痛みを感じ始めていますさらに悪いことに。

ここでの私の主な質問/不満は、異なる高さとサブビューを持つカスタムセルで複雑なテーブルを構築する最良の方法と、サイズを計算する方法、またはそれよりも優れていることですheightForRowAtIndexPath。サイズを取得するために画像をダウンロードしました。レイアウト、再描画、および「サイズに合わせた」ための多くのプロパティがありますが、それらのいずれも、私が想定していたように機能していないようです。iOSアプリのレイアウトがどのように機能するかを知るには遠すぎると感じており、UI要素にはまったく柔軟性がないように思えます:(

これは使用されるソース コードです (ちなみに、画像を取得して UIImageView に読み込むために AFNetworking ライブラリを使用しています)。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"PostCell";
     NewsFeedPostCell *cell;

    NSDictionary *post = [self.posts objectAtIndex:indexPath.row];
    NSString *postText = [post objectForKey:@"longDescription"];

    cell= (NewsFeedPostCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NewsFeedCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

 CGRect picFrame = cell.picView.frame;
    picFrame = CGRectMake(100, 0, 300, 100);
    [cell.picView setFrame:picFrame];
    [cell.picView  setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[post objectForKey:@"artworkUrl100"]]]
                        placeholderImage:nil
                                 success:^(NSURLRequest *request , NSHTTPURLResponse *response , UIImage *image ){
                                     NSLog(@"Loaded successfully: %d", [response statusCode]);
                                     [cell.picView setImage:image];
                                     [cell.picView sizeToFit];

                                 }
                                 failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
                                     NSLog(@"failed loading: %@", error);
                                 }
     ];

CGSize stringSize = [self calculateTextHeight:postText];

    CGRect postFrame = cell.postContent.frame;
    postFrame = CGRectMake(10, 100, 300, stringSize.height);
    cell.postContent.frame = postFrame;

    cell.postContent.autoresizingMask = UIViewAutoresizingFlexibleHeight;

    cell.postContent.font = [UIFont systemFontOfSize:POST_CONTENT_FONT_SIZE];
    cell.postContent.text= postText;

    [cell.postContent setContentInset:UIEdgeInsetsZero];

    [cell.postContent sizeThatFits:cell.postContent.contentSize];


    return cell;
}

スクリーンショットの問題を参照してください (私は 3.5 インチの Retina デバイスをシミュレートする iOS シミュレーターを使用し、アプリ iOS SDK 6.1 をビルドするためのターゲットとして使用しています)

最初の行 (画像はトップバーの下にあります。表示するにはスクロールする必要があります) これが一行目

テキストが正しく表示されない別の行があります。取得するには上下にスクロールする必要があります。 ここに画像の説明を入力

ここに画像の説明を入力

そして、最後の行のテキストを完成させることは不可能です: ここに画像の説明を入力

4

2 に答える 2

2

高さの異なるカスタム セルを作成する最良の方法は次のとおりです。

セルの NSMutableArray を作成します。

@property (nonatomic,retain)NSMutableArray* cellsArray;

次にメートルで:

呼び出す最初のメソッドは

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

ここにセルを作成します。

        -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
            if (!self.cellsArray) {

                self.cellsArray = [[NSMutableArray alloc]init];

            }

            ListCell* cell = [[ListCell alloc] initWithFrame:CGRectMake(0, 0,, 50)];


           // IMPLEMENT your custom cell here, with custom data, with custom,different height:

           // after add this cell to your array:
            [self.cellsArray addObject:cell];

           // IMPORTANT STEP:  set your cell height for the new height:

            cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, 303, yourLastItemInCell.frame.origin.y + yourLastItemInCell.frame.size.height);


            return cell.frame.size.height;
        }

高さの異なるカスタムセルを取得した後:

#pragma mark - conform to delegates

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 10;
}

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


    return self.cellsArray[indexPath.row];

}

私はそれが役立つことを願っています!

于 2013-09-09T14:56:45.840 に答える