0

我有一个 UITableView 应该显示新闻。我确实设置了 2 个自定义单元格 -> 一个有没有图像的新闻,另一个有带有图像的新闻。所以细胞需要不同的高度。在情节提要中,我创建了这 2 个单元,它们每个都有一个自定义高度(280 有图像,130 没有)。

出于测试目的,我希望第一个和第三个单元格是新闻单元格 + 图像。这就是我的代码:

- (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 回答