【问题标题】:How to set the Cell height as dynamically in iPhone?如何在 iPhone 中动态设置单元格高度?
【发布时间】:2011-06-05 05:48:02
【问题描述】:

在我的应用程序中,我创建了表格视图并在表格视图中显示了图像和标签。我已经下载了图像并将图像高度设置到另一个类中,并且我已经使用委托传递了图像高度值。在我的问题中,heightForRowAtIndexPath 方法被调用得非常早,并且它永远不会再次调用。那么我如何设置单元格高度取决于图像高度?

//appDelegate.imageHeight - 图片高度委托值。

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

 NSLog(the height of the image is --------------------------%f, appDelegate.imageHeight);
 In Console:
     the height of the image is --------------------------0.000000
     the height of the image is --------------------------0.000000
     the height of the image is --------------------------0.000000
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     NSLog(the height of the image is --------------------------%f, appDelegate.imageHeight);
     In Console:

        the height of the image is --------------------------98.567039
        the height of the image is --------------------------300.000000
        the height of the image is --------------------------232.288406
 }

是否可以在 cellForRowAt 方法中调用 heightforRowAtIndexPath 方法。因为我设置了单元格框架的大小和高度取决于图像高度。那么如何设置单元格高度取决于图像高度?请指导我。

谢谢!

【问题讨论】:

  • 我正在尝试向应用添加类似的行为;找到了这个帖子,这可能有解决方案? - 答案是调用 [tableView beginUpdates]; [tableView endUpdates] 看起来很有希望? :stackoverflow.com/questions/460014/…

标签: iphone uitableview height cell


【解决方案1】:

【讨论】:

    【解决方案2】:

    heightForRowAtIndexPath 方法在 cellForRowAtIndexPath 之前调用,因此您需要知道在单元格中显示的图像的高度。维护包含传递给此类的图像高度的字典或数组,并使用它来相应地设置单元格的高度

    【讨论】:

    • 感谢您的回复。但是图像高度值仅在 cellForRowAtIndexPath 方法中获取,如果我创建任何字典或数组,那么如何将这些值传递给 heightForRowAtIndexPath 方法?那么是否有可能在 cellForRowAtIndex 方法或其他一些解决方案中调用“heightForRowAtIndexPath”?谢谢。
    • 怎么可能在你的视图出现时无法在 cellForRowAtIndex 中计算图像高度?告诉我你是如何得到你的图像的?通过解析什么的?你在 cellForRowAtIndex 路径中实际做了什么?
    • 是的 Nanda,我使用 Async 库来临时加载延迟加载。图像高度值被传递给另一个类,因此图像高度值仅获取表格视图单元格滚动,即 cellForRowAtIndex方法。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多