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?
}
изображение