我试图在我的ViewController上显示两个UICollectionViews,但是没有数据加载并且UICollectionViews为空

时间:2019-04-24 23:14:45

标签: swift xcode uicollectionview viewcontroller collectionview

我正在第一个ViewController上制作具有多个UICollection View的应用程序;但是,每当我在模拟器中运行我的应用程序时,两个CollectionView都不会显示任何项目。我想知道如何获取这些视图控制器以向我提供所需的视图。

我放入了打印语句,以查看程序是否告诉它们不带任何值返回,但是打印语句以正确的值返回:1和21。

编辑:

我认为问题出在功能后的ifStatement函数中的collectionViews,因为在我模拟时allDeals会加载,但是myFavotires仍然显示为空。

class ViewController: UIViewController {

    let allDiscounts = discountBank()
    let dateClass = theDate()
    let defaults = UserDefaults.standard
    var my1 = 0
    var isBoolLiked = true
    var myKey = ""
    var myEmptyArray = [Int]()
    let dealLikeNames = ["Starbucks", "Qdoba", "Boston Market", "Ritas", "Cinnabon", "Arbys", "AMCTheatres", "PretzelMaker", "PhillyPF", "KrispyKreme1", "DunkinDonuts", "ChickFilA", "711", "KrispyKreme2", "LongJohnSilvers", "KrispyKreme3", "Dennys", "TGIF", "OliveGarden", "GoldenCorral", "Quiznos"]
    var todaysDate = 0
    var k=0
    var k2=1
    var myList = [Int]()
    var labelOne = ""
    var labelTwo = ""
    var labelThree = ""
    let day = Calendar.current.component(.day, from: Date())
    let month = Calendar.current.component(.month, from: Date())

    struct searchStruct {
        static var buttonTag=0
    }

    @IBOutlet var allDealsCollectionView: UICollectionView!
    @IBOutlet var nextupdiscountview: UIView!
    @IBOutlet var myfavoritesview: UIView!
    @IBOutlet var myFavoritesCollectionView: UICollectionView!
    @IBOutlet var nextUpLogo: UIImageView!
    @IBOutlet var nextUpDate: UILabel!

    @IBAction func nextUpDiscountButton(_ sender: UIButton) {
        searchStruct.buttonTag = myList[0]
        performSegue(withIdentifier: "gotocompany1", sender: self)
    }

    override func viewDidLoad() {
        AppUtility.lockOrientation(.portrait)
        homeImage()
        findNumberOfLikes()
        myfavoritesview.layer.cornerRadius = 10
        nextupdiscountview.layer.cornerRadius = 10
        nextUpLogo.layer.cornerRadius = 10
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    func findNumberOfLikes() {
        for i in 0...dealLikeNames.count-1 {
            myKey = dealLikeNames[i]
            isBoolLiked = UserDefaults.standard.bool(forKey: myKey)
            if isBoolLiked == true {
                myEmptyArray.append(i)
            }
        }
    }

    func homeImage() {
        for i in 0...allDiscounts.list.count-1 {
            if dateClass.todaysDate > allDiscounts.list[i].dateApplied {
                my1 = i+1
            }
        }
        myList.append(my1)

        for i in my1+1...allDiscounts.list.count-1{
            myList.append(i)
        }
        if my1>=1{
            for i in 0...my1-1{
                myList.append(i)
            }
        }
        print(myList)

        nextUpLogo.image = UIImage(named: allDiscounts.list[myList[0]].imageName)
        nextUpLogo.layer.cornerRadius = 10

        nextUpDate.text = allDiscounts.list[myList[0]].dateText
    }
}

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView == self.myFavoritesCollectionView{
            print(myEmptyArray.count)
           return myEmptyArray.count
        }
        else {
            print(allDiscounts.list.count)
            return allDiscounts.list.count
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == self.myFavoritesCollectionView {
            let cell = myFavoritesCollectionView.dequeueReusableCell(withReuseIdentifier: "theLikes", for: indexPath) as? myLikesCollectionViewCell
            cell?.companyLikedImage.image = UIImage(named: allDiscounts.list[myEmptyArray[indexPath.row]].imageName)
            cell?.companyLikedName.text = allDiscounts.list[myEmptyArray[indexPath.row]].businessName
            return cell!
        } else {
            let cell = allDealsCollectionView.dequeueReusableCell(withReuseIdentifier: "allDeals", for: indexPath) as? allDealsCollectionViewCell
            cell?.companyLikedImage1.image = UIImage(named: allDiscounts.list[indexPath.row].imageName)
            cell?.companyLikedName1.text = allDiscounts.list[indexPath.row].businessName
            return cell!
        }
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if collectionView == self.myFavoritesCollectionView {
            // handle tap events
            ViewController.searchStruct.buttonTag = myEmptyArray[indexPath.row]
            performSegue(withIdentifier: "gotocompany1", sender: self)
        } else {
            ViewController.searchStruct.buttonTag = indexPath.row
            performSegue(withIdentifier: "gotocompany1", sender: self)
        }
    }
}

我希望allDealsCollectionView中有21个项目,而myFavoritesCollectionView中有1个项目,但是现在它们都没有任何返回。

0 个答案:

没有答案