0

ニュースを表示する UITableView があります。2 つのカスタム セルを設定しました -> 1 つには画像のないニュースがあり、もう 1 つのセルには画像付きのニュースがあります。したがって、セルには異なる高さが必要です。ストーリーボードでこれらの 2 つのセルを作成し、それぞれにカスタムの高さ (画像ありで 280、画像なしで 130) を設定しました。

テスト目的で、1 番目と 3 番目のセルをニュース セル + 画像にしたいと考えています。それが私のコードです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary * news = [postArray objectAtIndex:indexPath.row]; //PostArray is the array with the news
NSDictionary *authorData = [news valueForKey:@"author"];


NSString *postTitle = [self decodedString:[news valueForKey:@"title"]]; 
//Decode = change html chars
NSString *postExcerpt = [self decodedString:[news valueForKey:@"excerpt"]];
NSString *postComments = [news valueForKey:@"comment_count"];
//NSString *postID = [news valueForKey:@"id"];


NSString *authorFirstName = [self decodedString:[authorData valueForKey:@"first_name"]];
NSString *authorLastName = [self decodedString:[authorData valueForKey:@"last_name"]];
NSString *authorNickName = [self decodedString:[authorData valueForKey:@"nickname"]];

if (indexPath.row == 0 || indexPath.row == 2){



    NewsCellImage *cell = (NewsCellImage *)[tableView dequeueReusableCellWithIdentifier:@"NewsCellImage"];

    if (cell == nil) {
        cell = [[NewsCellImage alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NewsCellImage"];

    }


    // Set up the cell
    cell.postTitle.text = postTitle;
    cell.postExcerpt.text = postExcerpt;
    cell.postCommentsCount.text = [NSString stringWithFormat:@"%i",indexPath.row];

    //Check if names are empty
    if ([authorFirstName length] == 0 && [authorLastName length] == 0){
        cell.postMeta.text = [NSString stringWithFormat:@"%@ / %@",authorNickName,[news valueForKey:@"date"]];
    } else {
        cell.postMeta.text = [NSString stringWithFormat:@"%@ %@ / %@",authorFirstName,authorLastName,[news valueForKey:@"date"]];
    }

    //Set Image
    NSDictionary *imageData = [news valueForKey:@"thumbnail_images"];
    NSDictionary *imageDataFull = [imageData valueForKey:@"large"];
    NSString *postImage = [imageDataFull valueForKey:@"url"];


    [cell.postImage setImageWithURL:[NSURL URLWithString:postImage]
                   placeholderImage:[UIImage imageNamed:@"menu"]
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                              if (error){
                                  NSLog(@"Error bei Bild laden: %@",postTitle);
                              }

                          } usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];



    return cell;
} else {

    NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:@"NewsCell"];

    if (cell == nil) {
        cell = [[NewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NewsCell"];

    }


    // Set up the cell
    cell.postTitle.text = postTitle;
    cell.postExcerpt.text = postExcerpt;
    cell.postCommentsCount.text = [NSString stringWithFormat:@"%@",postComments];

    //Check if names are empty
    if ([authorFirstName length] == 0 && [authorLastName length] == 0){
        cell.postMeta.text = [NSString stringWithFormat:@"%@ / %@",authorNickName,[news valueForKey:@"date"]];
    } else {
        cell.postMeta.text = [NSString stringWithFormat:@"%@ %@ / %@",authorFirstName,authorLastName,[news valueForKey:@"date"]];
    }



    return cell;
}


}

コードは一般的に機能し、アプリを起動した後に表示されるものです: http://cl.ly/image/320K1y081T3Z

しかし、画像のある次のセルまで下にスクロールすると、何か問題が発生します: http://cl.ly/image/2M2O1X3R2T13

コードに何か問題があるか、何かを追加する必要がありますが、どちらの方法を取るべきかわかりません。

たぶんそれは

  -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{ }

しかし、私は本当に知りません。助けていただければ幸いです。タイ

4

2 に答える 2

1

この方法を試してください。

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{ 

if (indexPath.row == 0 || indexPath.row == 2){
 return newsWithImageHeight; 
}
 else  return newsHeight; 


}

または、interfacebuilder でセルの自動レイアウト設定を確認できます

于 2013-07-18T11:45:36.657 に答える
0

どのようにあなたの:

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{ }

見て?

次のようなものが必要だと思います:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{
if (indexpath.row == 0 || indexpath.row == 2) {
    return 280;
}
return 130;

}

于 2013-07-18T11:42:17.250 に答える