如何在 iPhone 中动态设置 Cell 高度?
在我的应用程序中,我创建了表格视图并在表格视图中显示图像和标签。我已经下载了图像并将图像高度设置到另一个类中,并且我已经使用委托传递了图像高度值。我的问题是,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 方法。因为我已经设置了单元格框架大小和高度取决于图像高度。那么我如何根据图像高度设置单元格高度?请指导我。
谢谢!
In my apps, i have created table view and displayed the images and the labels in the table view. I have downloaded the image and set the image height into the another class and i have passed the image height value using delegates. In my problem is, heightForRowAtIndexPath method is call very earlier and it never calls again. So how i set the cell height depends on the image height?
//appDelegate.imageHeight - Image height delegate value.
- (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
}
Is any possible to call the heightforRowAtIndexPath method inside the cellForRowAt method. Because i have set the cell frame size and height depends on the image height. So how can i set the cell height depends on the image height?, please guide me.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
heightForRowAtIndexPath 方法在 cellForRowAtIndexPath 之前调用,因此您需要知道单元格中显示的图像的高度。维护包含传递给此类的图像高度的字典或数组,并使用它相应地设置单元格的高度
heightForRowAtIndexPath method is called before cellForRowAtIndexPath, so you need to know height of images you are displaying in your cells. Maintain the dictionary or array that contains height of images which get passed to this class and use it to set the height of cell accordingly
这里有一些建议UITableView灵活/动态heightForRowAtIndexPath
here are some suggestions UITableView flexible/dynamic heightForRowAtIndexPath