【问题标题】:dynamic height for image and cell in uitableview autoresizinguitableview自动调整大小中图像和单元格的动态高度
【发布时间】:2016-08-21 04:51:36
【问题描述】:

如何在UITableView Autoresizing 中为image 和cell 使用set 和dynamic height。我在Storyboard 中使用了UI 的自动调整大小。我正在从网络服务获取图像的静态宽度和动态高度的图像,例如 Facebook。谁能帮我设置图像和单元格的动态高度。

【问题讨论】:

  • 现在我对图像和 uitableviewcell 都使用静态高度。我需要像 facebook 一样为图像和 uitableviewcell 设置动态高度。
  • 在故事板中对 Imageview 的左、右、上、下角进行约束,并在 tableviewcell 中设置您的高度。它会自动工作

标签: ios uitableview dynamic height autoresize


【解决方案1】:

你可以使用这个方法动态改变tableviewcell的高度

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
 UIImage *img = [UIImage imageNamed:@"yourImage"];
 return img.size.height;
}

如果您在下载后拥有 imageurl,请使用下面的代码更新高度

UIImage *img=[UIImage imageWithData:data];

[self.tableView beginUpdates]; [self.tableView endUpdates];

【讨论】:

    【解决方案2】:
    - (void)setDefaultRowHeights 
    {
        self.imageHeights = [NSMutableArray arrayWithCapacity:self.imageURLs.count];
        for (int i = 0; i < self.imageURLs.count; i++) {
            self.imageHeights[i] = @(self.tableView.rowHeight);
        }
    }
    - (void)setDefaultRowHeights {
        self.imageHeights = [NSMutableArray arrayWithCapacity:self.imageURLs.count];
        for (int i = 0; i < self.imageURLs.count; i++) {
            self.imageHeights[i] = @(self.tableView.rowHeight);
        }
    }
    

    然后,让我们转到 tableView:cellForRowAtIndexPath: 下载图像并将它们的高度存储在我们的 imageHeights 数组中(这是一个很长的数组,但不要害怕)。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        DynamicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DynamicCell" forIndexPath:indexPath];
        NSString *imageURL = self.imageURLs[indexPath.row];
        __weak DynamicTableViewCell *weakCell = cell;
        __weak typeof(self) weakSelf = self;
        [cell.mainImageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]]
                                  placeholderImage:[UIImage new]
                                           success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                               weakCell.mainImageView.image = image;
                                               // Update table row heights
                                               NSInteger oldHeight = [weakSelf.imageHeights[indexPath.row] integerValue];
                                               NSInteger newHeight = (int)image.size.height;
                                               // If image is wider than our imageView, calculate the max height possible
                                               if (image.size.width > CGRectGetWidth(weakCell.mainImageView.bounds)) {
                                                   CGFloat ratio = image.size.height / image.size.width;
                                                   newHeight = CGRectGetWidth(self.view.bounds) * ratio;
                                               }
    
                                               // Update table row height if image is in different size
                                               if (oldHeight != newHeight) {
                                                   weakSelf.imageHeights[indexPath.row] = @(newHeight);
                                                   [weakSelf.tableView beginUpdates];
                                                   [weakSelf.tableView endUpdates];
                                               }
                                           } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                               NSLog(@"Error: %@\nFetching image with url: %@", error, request.URL);
                                           }];
    
        return cell;
    }
    

    简单!我们现在可以在 tableView:heightForRowAtIndexPath: 方法中返回正确的值,如下所示:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return [self.imageHeights[indexPath.row] integerValue];
    }
    

    正确重新加载表格视图单元格

    尝试尝试几种不同的方法。我发现当单元格高度改变时,只调用 beginUpdates 和 endUpdates 会产生最好的动画/过渡。您也可以在这两者之间调用 reloadRowsAtIndexPaths:withRowAnimation: 但它会进行额外的刷新并且 UI 看起来很糟糕。

    【讨论】:

    • 我已经实现了这个方法。但我的回复可能同时包含图像和文档 URL。我验证了它,但我无法获得图像的 tableview 的确切索引行。返回错误 [self.imageHeights[indexPath.row] integerValue].
    • 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayM objectAtIndex:]: index 7 beyond bounds [0 .. 5]”。
    • 你的数组值可能是 NIl.. 所以这个错误出现在返回 [self.imageHeights[indexPath.row] integerValue]。用断点检查完整代码..@MukeshN
    • if ([[[[dashboardList objectAtIndex:indexPath.row] valueForKey:@"file"] valueForKey:@"type"] isEqualToString:@"image"]) { [self.imageHeights[indexPath .row] integerValue]);返回 [self.imageHeights[indexPath.row] integerValue]; } 其他 { 返回 400; }
    • 我发现了问题。我在 tableview 中只有 10 行中的 6 张图像。即使我从上面的代码中验证了 heightForRowAtIndexPath。问题是进入 tableview indexpath.row。
    【解决方案3】:

    您可以使用此方法提供 UITableViewCell 的动态高度

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        return yourvalue;
    }
    

    【讨论】:

      【解决方案4】:

      您需要实现UITableViewDelegate 和UITableViewDataSource 方法来执行此操作,还需要在heightForRowAtIndexPath(UITableViewDelegate 方法)中为每个单元格计算单元格的高度。

      【讨论】:

      • uitableviewcell中如何设置动态图片高度?
      • 为此,您需要在运行时计算/获取图像高度并从heightForRowAtIndexPath 方法返回值。
      • 运行时如何计算图片高度?
      猜你喜欢
      • 2019-08-15
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多