带有 UIImageView 的 Swift 自定义 UITableViewCell.需要将单元格大小调整为图像高度

2024-04-15移动开发问题
3

本文介绍了带有 UIImageView 的 Swift 自定义 UITableViewCell.需要将单元格大小调整为图像高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试制作应用程序,它将在表格视图中显示图像.我有带有图像视图的自定义单元格.它只需要从 url 下载图片数据:

I'am trying to make app, that will show images in table view. I have custom cell with image view. It only must download image data from url:

@IBOutlet weak var tweetImage: UIImageView!
var imageData : MediaItem? { didSet { updateUI() }}

func updateUI(){
    tweetImage.image = nil
    if let url = imageData?.url {
        if let data = NSData(contentsOfURL: url) {
            tweetImage.image = UIImage(data: data)
        }
    }
}

我需要在下载后更改单元格高度.它必须等于图像的高度.我在 viewController 中设置了自动尺寸:

I need to get the cell height was changed after the download. It must be equal to the height of the image. I set auto dimension in viewController:

override func viewDidLoad() {
        super.viewDidLoad()
        tableView.estimatedRowHeight = tableView.rowHeight
        tableView.rowHeight = UITableViewAutomaticDimension
    }

我将aspect fit"设置为图像,我得到了奇怪的结果.图像超出了单元格的边界.也许我需要设置一些约束……我不知道.

I set "aspect fit" to image, I get strange results. The image extends beyond the boundaries of the cell. Maybe i need to set some constraints... I don't know.

结果:

但我需要这个:

推荐答案

如果要异步加载图片:

加载并设置新的 UIImage 后,您可以通过 UITableView 函数重新加载特定的单元格:

After you load and set new UIImage, you can reload specific cell via UITableView function:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

在你的 UIViewController 中:

In your UIViewController:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.estimatedRowHeight = tableView.rowHeight
    tableView.rowHeight = UITableViewAutomaticDimension
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "imageDidLoadNotification:", name:"CellDidLoadImageDidLoadNotification", object: nil)
}

func imageDidLoadNotification(notification: NSNotification) {
    if  let cell = notification.object as? UITableViewCell
        let indexPath = tableView.indexPathForCell(cell) {
        tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
    }
}

在你的 UITableViewCell 中

In your UITableViewCell

func updateUI(){
    tweetImage.image = nil
    if let url = imageData?.url {
        if let data = NSData(contentsOfURL: url) {
            tweetImage.image = UIImage(data: data)
            NSNotificationCenter.defaultCenter().postNotificationName("CellDidLoadImageDidLoadNotification", object: self)
        }
    }
}

这篇关于带有 UIImageView 的 Swift 自定义 UITableViewCell.需要将单元格大小调整为图像高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)...
2024-08-12 移动开发问题
13