Swift link Image from Parse array using segues to secondViewController

爱⌒轻易说出口 提交于 2020-01-11 11:59:11

问题


Here is my code, i am trying to use the "prepareForSegue" function to send an image from tableViewController (firstViewController) to my detailedViewController (secondViewController). I have managed to populate my firstViewController from the parse cloud successfully and I have managed to get my secondViewController Labels to update, but i can not get the imageView to update. I have posted my code below

firstViewController

    override func prepareForSegue(segue: UIStoryboardSegue, sender:      AnyObject?) {

    let eventDetailVC: EventDetailsVC = segue.destinationViewController as!  EventDetailsVC
    if let selectedArrayIndex = tableView.indexPathForSelectedRow?.row {

        eventDetailVC.detailNameLabel = postedEvents[selectedArrayIndex]
        eventDetailVC.detailAddressLabel = postedAddress[selectedArrayIndex]
        eventDetailVC.detailCityLabel = postedCity[selectedArrayIndex]
        eventDetailVC.detailStateLabel = postedState[selectedArrayIndex]
        eventDetailVC.detailStartLabel = postedStart[selectedArrayIndex]
        eventDetailVC.detailEndLabel = postedEnd[selectedArrayIndex]
        eventDetailVC.detailPriceLabel = postedPrices[selectedArrayIndex]
        eventDetailVC.detailDescriptionLabel =    postedDescription[selectedArrayIndex]

        // The error is here....i think
        eventDetailVC.detailImageView.image = image

    }
}

secondViewController

   var detailNameLabel = String()
   var detailDescriptionLabel = String()
   var detailPriceLabel = String()
   var detailStartLabel = String()
   var detailEndLabel = String()
   var detailAddressLabel = String()
   var detailCityLabel = String()
   var detailStateLabel = String()
   var detailImageView = UIImage()

   override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    detailName.text = detailNameLabel
    detailDescription.text = detailDescriptionLabel
    detailPrice.text = detailPriceLabel
    detailStart.text = detailStartLabel
    detailEnd.text = detailEndLabel
    detailAddress.text = detailAddressLabel
    detailCity.text = detailCityLabel
    detailState.text = detailStateLabel

    // its this line below....i think
    detailImage.image = detailImageView
    }

Please can someone help me figure this out, Im kinda new to this whole thing


回答1:


Hers the save code i think you wanted to see...

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! EventsCell

    postedImages[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in

        if let downloadedImage = UIImage(data: data!) {

            myCell.postedEventImage.image = downloadedImage
        }
    }
    myCell.eventName.text = postedEvents[indexPath.row]
    myCell.eventStart.text = postedStart[indexPath.row]
    myCell.eventPrice.text = postedPrices[indexPath.row]

    return myCell
}

and then there is also this code on how i append to my postImages array

     if let objects = objects {
                for object in objects {

                    self.postedImages.append(object.objectForKey("Image") as! PFFile)

                    self.tableView.reloadData()
                }



回答2:


It looks like you should define a UIImageView instead of a UIImage in secondViewController. Your detailImageView will need to be of type UIImageView. The way you have it written, it is of type UIImage.

In secondViewController, replace

var detailImageView = UIImage()

with

var detailImageView = UIImageView()

Then change:

// its this line below....i think
detailImage.image = detailImageView

to

// its this line below....i think
detailImage.image = detailImageView.image

To handle the image sending from the table view cell to the second view controller will require grabbing the image from the selected cell. This would look something like this in your prepareForSegue:

if let myIndexPath = tableView.indexPathForSelectedRow? {
    let cell = self.tableView.cellForRowAtIndexPath(myIndexPath)
    eventDetailVC.detailImageView.image = cell.postedEventImage.image 
}

Your other assignments in viewDidLoad in secondViewController are also a bit unusual. You appear to be assigning the text values from UILabels based on how you are naming them. If a variable is a UILabel than having it named somethingLabel is appropriate. However, if the type is a String, which they are based on your variable assignments, than having it named somethingLabel can be a source of confusion.



来源:https://stackoverflow.com/questions/33562148/swift-link-image-from-parse-array-using-segues-to-secondviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!