Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView+;2个自定义单元格=高度错误_Ios_Height_Tableview_Cell - Fatal编程技术网

Ios UITableView+;2个自定义单元格=高度错误

Ios UITableView+;2个自定义单元格=高度错误,ios,height,tableview,cell,Ios,Height,Tableview,Cell,我有一个可以显示新闻的视图。 我设置了两个自定义单元格->一个有没有图像的新闻,另一个有图像的新闻。 所以细胞需要不同的高度。在故事板中,我创建了这两个单元,每个单元都有一个自定义的高度(280有图像,130没有图像) 出于测试目的,我希望第一个和第三个单元格是新闻单元格+图像。 这就是我的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPa

我有一个可以显示新闻的视图。 我设置了两个自定义单元格->一个有没有图像的新闻,另一个有图像的新闻。 所以细胞需要不同的高度。在故事板中,我创建了这两个单元,每个单元都有一个自定义的高度(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;
}


}
代码在一般情况下确实有效,这就是我在启动应用程序后看到的:

但当我向下滚动到下一个有图像的单元格时,出现了一些问题:

所以我的代码有问题,或者我需要添加一些东西,但我不知道该怎么做

也许这是因为

  -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{ }
但我真的不知道。我感谢你的帮助。Ty

您的:

 -(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;
}试试这个方法

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

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


}

或者您可以在interfacebuilder中检查单元格的自动布局设置

谢谢,我会将两个响应标记为正确的解决方案,但我会投票支持您的:)好的,那么我会使用它,尽管我的代码中有其他方法或问题。谢谢