I am trying to use the detail disclosure button on the view controller pictured below (PhotoSearchViewController) to show a UIAlertController that shows the photo description from the ImageUrlItem.
When I click the button, it shows that it was pressed, but the alert containing the description does not appear.
My code for the ImageUrlItem:
import Foundation
import Firebase
struct ImageUrlItem {
//Should I add my description in here?
let key: String
let imageUrl: String
let watsonCollectionImageUrl: String
let score: Double
let description: String
let ref: FIRDatabaseReference?
init(imageUrl: String, key: String = "", watsonCollectionImageUrl: String = "", score: Double = 0, description: String) {
self.key = key
self.imageUrl = imageUrl
self.watsonCollectionImageUrl = watsonCollectionImageUrl
self.ref = nil
self.score = score
self.description = description
}
init(snapshot: FIRDataSnapshot) {
key = snapshot.key
let snapshotValue = snapshot.value as! [String: AnyObject]
imageUrl = snapshotValue["ImageUrl"] as! String // must map to firebase names
watsonCollectionImageUrl = ""
score = 0
description = snapshotValue["Description"] as! String
ref = snapshot.ref
}
func toAnyObject() -> Any {
return [
"imageUrl": imageUrl,
"Description": description
]
}
}
My code for the collection view cell (PhotoCell) is as follows:
class PhotoCell: UICollectionViewCell, UIAlertViewDelegate
{
var photos: [ImageUrlItem] = []
var ref = FIRDatabase.database().reference(withPath: "Photos")
@IBOutlet weak var imgPhoto: UIImageView!
@IBOutlet weak var lblScore: UILabel!
@IBAction func btnDesc(_ sender: UIButton)
{
let alertTitle = "Description"
let alertMessage = photos.description
let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title:"Ok", style: UIAlertActionStyle.default)
{
(result : UIAlertAction) -> Void in
print("OK")
}
alertController.addAction(okAction)
self.parentViewController?.present(alertController, animated: true, completion: nil)
}
}
Code for the cellForRowAt in my PhotoSearchViewController:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let reuseIdentifier = "PhotoCell"
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! PhotoCell
cell.backgroundColor = UIColor(red:0.74, green:0.76, blue:0.78, alpha: 1.0)
// Do any custom modifications you your cell, referencing the outlets you defined in the Custom cell file // if we have a label IBOutlet in PhotoCell we can customize it here
// on page load when we have no search results, show nothing
if similarImageUrls.count > 0 {
//print(indexPath.row)
//print(similarImageUrls.count)
if (indexPath.row < similarImageUrls.count){
let image = self.similarImageUrls[indexPath.row]
// get image asynchronously via URL
let url = URL(string: image.imageUrl)
DispatchQueue.global().async {
// make an asynchonorous call to load the image
DispatchQueue.main.async {
cell.imgPhoto.af_setImage(withURL: url!) // show image using alamofire
}
}
cell.lblScore.isHidden = false
cell.lblScore.text = "Score: \(NSString(format: "%.2f", (image.score * 100)) as String)%"
}
else
{
// show the placeholder image instead
cell.imgPhoto.image = UIImage(named: "person")
cell.lblScore.isHidden = true
cell.lblScore.text = "0.00%"
}
}
else
{
// show the placeholder image instead
cell.imgPhoto.image = UIImage(named: "person")
cell.lblScore.isHidden = true
cell.lblScore.text = "0.00%"
// when we get to the last image, and it is not the first time load
if (indexPath.row == 8 && !firstTimeSearch){
// show nothing found alert here
let ac = UIAlertController(title: "Photo Search Completed!", message:"No macthing photo found!", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
self.present(ac, animated: true)
}
}
return cell
}
}
You can use custom delegate to show the alert
Code for the
cellForRowAt
in PhotoSearchViewController: