简体   繁体   English

Eureka:自定义行的动态高度

[英]Eureka: Dynamic height of custom row

I've made a custom cell, that should only has an UIImageView .我制作了一个自定义单元格,它应该只有一个UIImageView The cell should change the height to fits the image, when the image is changed:当图像更改时,单元格应更改高度以适合图像:

import UIKit
import Eureka

public class FirebaseImageCell: Cell<UIImage>, CellType{

    @IBOutlet weak var customImageView: UIImageView!

    public override func setup() {
        super.setup()
    }

    public override func update() {
        super.update()

        guard let image = row.value else { return }
        customImageView.image = image

        guard let cellWidth = UIApplication.shared.keyWindow?.frame.width else { return }
        height = { cellWidth * ( image.size.height / image.size.width ) }
    }

}

public final class FirebaseImageRow: Row<FirebaseImageCell>, RowType {
    required public init(tag: String?) {
        super.init(tag: tag)
        cellProvider = CellProvider<FirebaseImageCell>(nibName: "FirebaseImageCell")
    }
}

I create the row like this:我创建这样的行:

let image = UIImage(named: "testImage")!

let testSection = Section("Test Section")
    <<< FirebaseImageRow("firebaseImageRow") { row in
            row.value = image
        }
form +++ testSection

At some point I want to change the image in the cell, and the cell should change it's height to fit the image.在某些时候,我想更改单元格中的图像,并且单元格应该更改其高度以适合图像。 Here I change the image:在这里,我更改图像:

func imageChange() {

    guard let imageRow = form.rowBy(tag: "firebaseImageRow") as? FirebaseImageRow else { return }
    let image = UIImage(named: "testImage2")        
    imageRow.value = image
    imageRow.updateCell()

}

When the form is initialized the cell gets the right height, fittet to "testImage", but the height stay the same after the image is changed.当表单初始化时,单元格获得正确的高度,fittet 为“testImage”,但在图像更改后高度保持不变。

How can I make the cell change it's height dynamically?我怎样才能让单元格动态改变它的高度?

Not sure if I answered you question, maybe you could try my function:不确定我是否回答了您的问题,也许您可​​以尝试我的功能:

(1)You could create a NSMutableDictionary to remember the cell's height. (1)您可以创建一个 NSMutableDictionary 来记住单元格的高度。 When the height are changed then reload tableView or section or the indexPath.当高度改变时,重新加载 tableView 或 section 或 indexPath。

(2) You also could user ReactiveCocoa to resolve that problem. (2) 你也可以使用 ReactiveCocoa 来解决这个问题。 And first of all,I suggest you to know about KVO, MVVM, Hook首先,我建议你了解 KVO、MVVM、Hook

(3) Our project set active image url like that: http://example.com/data/img.gif?width=271&height=360 (3) 我们的项目设置活动图片网址如下: http : //example.com/data/img.gif?width=271&height=360

so we can easy get the active imageView's size and update "heightForRowAtIndexPath:" function.所以我们可以轻松获取活动图像视图的大小并更新“heightForRowAtIndexPath:”函数。

在此处输入图片说明 在此处输入图片说明

You might try it out that alternative:你可以试试这个替代方案:

<<< FormTextRow(tag: "phone", title: "Teléfono", isPhone: true).cellUpdate({ [weak self] cell, row in
         row.add(rule: RuleRequired(msg: "Debes indicar tu teléfono", id: "phone required"))
         row.add(rule: RuleMinLength(minLength: Locale.current.regionCode ?? "" == "CL" ? 6 : 10, msg: "Debes ingresar un número celular de 8 dígitos", id: "cell invalid"))
         if (self?.params["phone"] as? String ?? "") != row.value ?? "" {
             self?.params["phone"] = row.value ?? ""
             self?.updateParams?(self?.params ?? [:])
             cell.setupErrorMessage()
             self?.tableView.reloadData()
         }
     })

NOTE: params are the params to complete the form, setupErrorMessage is the rendering method and reloadData is for appears message purpose注意: params是完成表单的参数, setupErrorMessage是渲染方法, reloadData是为了出现消息的目的

Happy coding快乐编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM