Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

ios - The image in the collection view controller not working properly

The project that I worked in is making memes then save it in the album and appear in the table view and collection using shared modal, but I have a problem with the collection view, the problem is that the images not appearing in the collection view all the time. when I add the memes in the table view it will be installed in the collection view, but when I switch to collection view and add from there it will be saved in the album but not appearing in the collection view!! and when i switch to the table after the collection view and try to add the memes it will not be added in the collection again! I do not know what's the problem is it in the code or the storyboard?

This is the code for the collection view:

import UIKit

// MARK: - SetMemeTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate

class SentMemeTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

    

    //MARK: Setting memes property to memes array in AppDelgate:
    var memes: [Meme]! {
        let object = UIApplication.shared.delegate
        let appDelegate = object as! AppDelegate
        return appDelegate.memes
    }
    
    //MARK: Adding plus icon(for adding memes)
    //view didload for putting the plus icon in the navigation bar
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add Meme", style: .plain, target: self, action: #selector(SentMemeTableViewController.AddMemeImage))
        
    }
    //MARK: THE PLUS ICON MEHTOD (IN THE RIGHT CORNER)
    @objc func AddMemeImage(){
        
        //This for hidding the tabBar and the navigationBar
        self.tabBarController?.tabBar.isHidden = true
        self.navigationController?.isNavigationBarHidden = true
        
        //Calling MemeEditorViewController using navigation
            let storyboard = UIStoryboard (name: "Main", bundle: nil)
            let resultVC = storyboard.instantiateViewController(withIdentifier: "MemeMeEditorViewController")as! MemeMeEditorViewController
        
            // Communicate the meme
       // resultVC.meme = self.memes
        
        
        //this is how to do navigation programmaticaly
        self.navigationController?.pushViewController(resultVC, animated: true)
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        //This for showing the tabBar and the navigationBar
        self.tabBarController?.tabBar.isHidden = false
        self.navigationController?.isNavigationBarHidden = false
            print("memes: (memes.count)")

    }
    
    // MARK: Table View Data Source methods
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return memes.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell")!
        let meme = self.memes[(indexPath as NSIndexPath).row]
        
        // Set the topText and the bottomText written in the right side of the image
        cell.textLabel?.text = meme.topText + " - " + meme.bottomText
        // Set the meme in the table view
        cell.imageView?.image = meme.memedImage
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let detailController = self.storyboard!.instantiateViewController(withIdentifier: "MemeDetailViewController") as! MemeDetailViewController
        detailController.meme = self.memes[(indexPath as NSIndexPath).row]
        navigationController!.pushViewController(detailController, animated: true)
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...