要显示来自api且位于xib文件中的collectionview单元格数据

时间:2019-07-08 15:51:14

标签: swift xcode api tableview

我创建了要查看集合视图的tableview单元xib,对于集合视图单元,我创建了另一个由图标和标签组成的xib。在主表视图中,我有不同的标题部分,如“ FOOD”,“ TRAVEL”等等。该标题也来自我已经解析过的api,但是每个类别中的数据都不知道如何解析。我的应用的屏幕截图如下:

screenshot of my app

我想要实现的示例api:

{
    "Result": [
        {
            "category_id": 1,
            "category_name": "FOOD",
            "created_date": "2019-07-06 12:16:17",
            "FOOD": [
                {
                    "card_id": 1,
                    "card_category": "1",
                    "card_name": "Fruits",
                    "card_icon": "0f6d3734f57a729e086090be3f5fbf67.jpeg",
                    "card_audio": "087976be634266a303bd65b8e3f250cc.mp3",
                    "card_status": "1",
                    "created_date": "2019-07-06 12:55:40",
                    "isFavorite": 0
                },
                {
                    "card_id": 2,
                    "card_category": "1",
                    "card_name": "Vegetables",
                    "card_icon": "403d14b91ad07f7f84442073cae56dcb.jpeg",
                    "card_audio": "cac38b7bd653d0a583015591fb637eda.mp3",
                    "card_status": "1",
                    "created_date": "2019-07-06 12:58:08",
                    "isFavorite": 1
                },

我已经解析了category_name,并且它也显示在标题标题中,但是“ FOOD”中的数据没有显示

我进行过tableview的homeview控制器代码:

class HomeViewController: UIViewController, NavgationTransitionable{

    @IBOutlet weak var cardsTableView: UITableView!
    @IBOutlet weak var homeSearchBar: UISearchBar!

    var cardCategories = [ModelGetCardsResult]()

    var deckFood = [ModelFood]()
    var deckSports = [ModelSports]()
    var deckTravel = [ModelTravel]()
    var deckBasics = [ModelBasics]()
    var deckAdjectives = [ModelAdjectives]()
    var deckSchool = [ModelAtSchool]()
    var deckPhrases = [ModelCommonPhrases]()
    var deckSchedule = [ModelSchedule]()
    var deckVerbs = [ModelVerbs]()
    var deckDating = [ModelDating]()

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

        //register external nib
        self.cardsTableView.register(UINib(nibName: "CategoryRow", bundle: nil), forCellReuseIdentifier: "CategoryRow")
        self.cardsTableView.register(UINib(nibName: "HeaderView", bundle: nil), forCellReuseIdentifier: "headerView")

        let nc = NotificationCenter.default
        nc.addObserver(self, selector: #selector(self.pushToFlipCardVC), name: Notification.Name("myNoti"), object: nil)

        self.setupSearchBar()

        self.WS_GetCards()

    }
//Get_cards API Parsing

    func WS_GetCards() {

        if ApiUtillity.sharedInstance.isReachable() {

            ApiUtillity.sharedInstance.StartProgress(view: self.view)

            APIClient<ModelBaseGetCards>().API_GET(Url: WS_GetCardsAPI, Params: [:], Authentication: true, Progress: true, Alert: true, Offline: false, SuperVC: self, completionSuccess: { (modelResponse) in

                if(modelResponse.success == true) {

                    ApiUtillity.sharedInstance.StopProgress(view: self.view)

                    self.cardCategories.removeAll()
                    if let tempCardCategories = modelResponse.getCardsResult, tempCardCategories.count != 0 {

                        for card in tempCardCategories {
                            self.cardCategories.append(card)
                        }
                    }

                }else {
                    self.showToast(message: modelResponse.message!)
                     ApiUtillity.sharedInstance.StopProgress(view: self.view)
                }
                self.cardsTableView.reloadData()

            }) { (failed) in
                self.showToast(message: failed.localizedDescription)
                 ApiUtillity.sharedInstance.StopProgress(view: self.view)
            }
        }else {
            self.showToast(message: "No internet connection...")
        }
    }
}

extension HomeViewController: UITableViewDataSource, UITableViewDelegate{

    func numberOfSections(in tableView: UITableView) -> Int {
        return cardCategories.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryRow", for: indexPath) as! CategoryRow

        return cell
    }


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cell = tableView.dequeueReusableCell(withIdentifier: "headerView") as! HeaderView

        let cardCatObj = cardCategories[section].categoryName

        if let headerTitle = cardCatObj, headerTitle.count != 0 {
           cell.titleHeader.text = headerTitle
        }
        return cell
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

       return 30
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }

}

我的tableview单元格代码:

class CategoryRow: UITableViewCell {

    @IBOutlet weak var cardsCollView: UICollectionView!

    var deckImagesArr: [String] = [] //"checkmark", "phrases", "checkmark"
    var deckTitleArr: [String] = [] //"Saying Hello & Saying Hello", "Common Phrases", "Common Phrases"

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.cardsCollView.register(UINib(nibName: "cardsCell", bundle: nil), forCellWithReuseIdentifier: "cardsCell")

    }
}

extension CategoryRow: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cardsCell", for: indexPath as IndexPath) as! cardsCell
        cell.popularTitles.text = deckTitleArr[indexPath.item]
        cell.popularImage.image = UIImage(named: deckImagesArr[indexPath.item])

        return cell

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return deckTitleArr.count
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let nc = NotificationCenter.default
        nc.post(name: Notification.Name("myNoti"), object: nil)
    }

}

extension CategoryRow: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize(width: (UIScreen.main.bounds.width - 18) / 2, height: 114)
    }
}

我的collectionview单元格:

class cardsCell: UICollectionViewCell {

    @IBOutlet weak var mainView: UIView!
    @IBOutlet weak var deckLock: UIButton!
    @IBOutlet weak var popularImage: UIImageView!
    @IBOutlet weak var popularTitles: UILabel!


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

        self.mainView.layer.cornerRadius = 6.0
        self.mainView.clipsToBounds = true
        self.mainView.layer.masksToBounds = false
        self.mainView.layer.borderWidth = 1.0
        self.mainView.layer.borderColor = UIColor(rgb: 0x1282F1).cgColor
    }

}

0 个答案:

没有答案