中使用详细信息披露按钮

中使用详细信息披露按钮

本文介绍了如何在 UICollectionViewCell 中使用详细信息披露按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下图所示的视图控制器 (PhotoSearchViewController) 上的详细信息披露按钮来显示 UIAlertController,该控制器显示来自 ImageUrlItem 的照片描述.

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.

我的 ImageUrlItem 代码:​​

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
        ]
    }

}

我的集合视图单元格(PhotoCell)代码如下:

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)
    }

}

我的 PhotoSearchViewController 中 cellForRowAt 的代码:

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

  // here is the protocol for creating the delegation:

    protocol BtnDesc : class {
        func btnDecAlert(alertTitle: String, message: String)
    }
 class PhotoCell: UICollectionViewCell
    {
        // MARK:- Delegate
         weak var btnDescDelegate : BtnDesc?

        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

           btnDescDelegate?.btnDecAlert(alertTitle: alertTitle, message: alertMessage)


        }

    }

PhotoSearchViewControllercellForRowAt 的代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let reuseIdentifier = "PhotoCell"
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! PhotoCell
            //Mark set the delegate.
            cell.btnDescDelegate  = self
     .....
  //conform delegate
  extension PhotoSearchViewController : BtnDesc {

        func btnDecAlert(alertTitle: String, message: String) {

            let alert = UIAlertController(title: alertTitle, message: message, preferredStyle: .alert)

            let okAction = UIAlertAction(title: "Ok", style: .default, handler: {(_ action: UIAlertAction) -> Void in

                  // here is your action
                    print("OK")

            })

            alert.addAction(okAction)

            self.present(alert, animated: true, completion: nil)
        }

    }

这篇关于如何在 UICollectionViewCell 中使用详细信息披露按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:31