EurekaCommunity / ViewRow

ViewRow is a Eureka row that allows you to display any UIView (or UIView sunclass) within a Eureka row. Views can be created in code or loaded from nib files.
MIT License
77 stars 16 forks source link

How to change image dynamically #14

Closed Oliver530 closed 6 years ago

Oliver530 commented 6 years ago

First of all, many thanks for the functionality. That's exactly what I was looking for.

How can I change the content of the series dynamically?

viewRowImage?.cell.view?.image = image has no effect on the viewDidAppear-Trigger. Updating the row with updateCell() or reload() does not help either.

I would be very happy about your help.

alldritt commented 6 years ago

How are you acquiring the viewRowImage value? Are you sure its non-nil? When I tested it here, all I need to do is assign a new image to the viewRowImage?.cell.view?.image. I've updated my example application to illustrate this - pull a fresh copy to see this in ImageViewViewController.swift.

Here's the example code I'm using:

            +++ Section("Image View with changable image")

                <<< SegmentedRow<String> { (row) in
                    row.title = "Image"
                    row.options = ["trees", "flower"]
                    row.value = "trees"
                }
                .cellSetup { (cell, row) in
                    cell.segmentedControl.setContentHuggingPriority(UILayoutPriority(rawValue: 750), for: .horizontal)
                    cell.segmentedControl.apportionsSegmentWidthsByContent = true
                }
                .onChange { [unowned self] (row) in
                    guard let imageName = row.value else { return }
                    guard let imageRow = self.form.rowBy(tag: "xxxx") as? ViewRow<UIImageView> else { return }

                    let image = UIImage(named: imageName)
                    imageRow.cell.view!.image = image
                }

                <<< ViewRow<UIImageView>("xxxx")
                .cellSetup { (cell, row) in
                    //  Construct the view for the cell
                    cell.view = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 300))
                    cell.view!.contentMode = .scaleAspectFill
                    cell.view!.clipsToBounds = true

                    //  Get something to display
                    let image = UIImage(named: "trees")
                    cell.view!.image = image

                    //  Make the image view occupy the entire row:
                    cell.viewRightMargin = 0.0
                    cell.viewLeftMargin = 0.0
                    cell.viewTopMargin = 0.0
                    cell.viewBottomMargin = 0.0
                }

Here's hat this looks like when running:

screenflow

Oliver530 commented 6 years ago

Thanks a lot for your super-fast help! I found the mistake on my side: I set the cell's tag in the cellSetup()-function via row.tag = "myID" instead of passing it to the constructor. This meant that I could not reference the row.

Thanks for updating the example!