如何在集合视图中快速创建动态按钮动作?

问题描述 投票:0回答:1
    import UIKit
    import Alamofire


    class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,StarSelect {        
    var user_Menu = [DynamicMenu]()
    var user_Id = [Int]()

    @IBOutlet weak var buton: UIButton!

    // UITableViewDelegate,UITableViewDataSource

    @IBOutlet weak var dashBoardCollectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        getData()                       
    }        

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return user_Menu.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"DasboardCell",for: indexPath) as! DasboardCell

        cell.nameTitle.text = user_Menu[indexPath.row].menuName            
        cell.delegate = self
        cell.indexPath = indexPath            
        return cell                     
    }


    func tapStar(indexPath: IndexPath, star: Int) {
       // let cell = ctsHistoryTableBiew.cellForRow(at: indexPath) as! CtcHistoryCell

        let cell = dashBoardCollectionView.cellForItem(at: indexPath) as! DasboardCell            
        if star == 1 {                
            print("profile")

        }            
        if star == 5 {                
            print("Attndnce")
        }            
    }


    func getData(){                

        let url = "https://www.url.com"
        Alamofire.request(url, method: .get, parameters:nil, encoding: URLEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
            switch(response.result) {
            case .success(_):                    
                if let data = response.result.value{                        
                    let json = response.result.value as? [String: Any]
                    print("json",json)
                    let responseStatus = json!["responseStatus"] as! Bool
                    let responseText  = json!["responseText"] as! String

                    if (responseStatus == true){

                        let responseData = json!["responseData"] as! [Any]
                        var index = 0
                        while index < responseData.count{
                            let responseArray = responseData[index] as! [String: Any]
                            let menuID = responseArray["MenuItemId"] as? Int
                            let menuName = responseArray["MenuItemName"] as? String

                            let folderApppend = DynamicMenu()
                            folderApppend.menuItemId = "\(menuID!)"
                            folderApppend.menuName = menuName
                            self.user_Menu.append(folderApppend)                                
                            self.user_Id.append(menuID!)                                
                            index += 1                                                             
                            print("userID",self.user_Id)                            
                        }

                        DispatchQueue.main.async {                                
                            self.dashBoardCollectionView.reloadData()                                
                        }                                                        
                    }                        
                }
                break                    
            case .failure(_):                   
                print("faliure",response.result.error!)
             //   self.displayAlertMessage(messageToDisplay: "Something Wrong Please try again")
                break

            }
        }               
    }              
}

import UIKit

protocol StarSelect {
    func tapStar(indexPath: IndexPath, star: Int)
}        

class DasboardCell: UICollectionViewCell {        

    @IBOutlet weak var nameTitle: UILabel!                      
    @IBOutlet weak var profileButton: UIButton!

    var delegate: StarSelect?
    var indexPath: IndexPath?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }        

    @IBAction func tapButton(_ sender: UIButton) {            
         delegate?.tapStar(indexPath: indexPath!, star: sender.tag)                        
    }        
}        

class DynamicMenu{        
    var menuItemId:String?
    var menuName:String?        
}

image

ios swift swift3
1个回答
0
投票

动态动作按钮是什么意思?

这样的东西?

//... 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"DasboardCell",for: indexPath) as! DasboardCell

            cell.nameTitle.text = user_Menu[indexPath.row].menuName
            cell.someCustomAction = { [weak self] in
                print(self?.user_Menu[indexPath.row].menuName)
            }
            return cell
        }

//... 

 class DasboardCell: UICollectionViewCell {

    @IBOutlet weak var nameTitle: UILabel!
    @IBOutlet weak var profileButton: UIButton!

    var someCustomAction: (() -> Void)?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    @IBAction func tapButton(_ sender: UIButton) {
        someCustomAction?()
    }
   }

//... 

一些观察:

  • 代理通常应该比较弱(由于内存泄漏)
  • 使用标记是一个不好的信号(总是根据您的情况进行自定义)
  • 您不需要将索引路径保留在单元格上(可以使用indexPathForCell方法)
© www.soinside.com 2019 - 2024. All rights reserved.