【问题标题】:how to navigate from a collection view after selecting didselectitem to another collection view选择 didselectitem 后如何从集合视图导航到另一个集合视图
【发布时间】:2017-08-24 11:49:04
【问题描述】:

我一直在尝试制作一个项目,其内容应该是 collectionView,在从中选择一个项目后会导航到另一个 collectionView

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var myCollectionView: UICollectionView!
var imgURLArray = [String]()

var nameArray = [String]()
final let urlString = "https://unembittered-vector.000webhostapp.com/JSON/alljson.txt"
override func viewDidLoad() {
    super.viewDidLoad()
    self.downloadJsonWithURL()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}
func downloadJsonWithURL() {
    let url = NSURL(string: urlString)
    URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in

        if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
            print(jsonObj!.value(forKey: "Materials")!)

            if let actorArray = jsonObj!.value(forKey: "Materials") as? NSArray {
                for actor in actorArray{
                    if let actorDict = actor as? NSDictionary {
                        if let name = actorDict.value(forKey: "name") {
                            self.nameArray.append(name as! String)
                        }/*
                        if let name = actorDict.value(forKey: "dob") {
                            self.dobArray.append(name as! String)
                        } */
                        if let name = actorDict.value(forKey: "image") {
                            self.imgURLArray.append(name as! String)
                        }

                    }
                }
            }


            OperationQueue.main.addOperation({
                self.myCollectionView.reloadData()
            })
        }
    }).resume()
}


func downloadJsonWithTask() {

    let url = NSURL(string: urlString)

    var downloadTask = URLRequest(url: (url as? URL)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 20)

    downloadTask.httpMethod = "GET"

    URLSession.shared.dataTask(with: downloadTask, completionHandler: {(data, response, error) -> Void in

        let jsonData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)

        print(jsonData!)

    }).resume()
}


//Number of views
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return imgURLArray.count

}

  //Populate view
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! myCell
    print("test")
    cell.nameLabel.text = nameArray[indexPath.row]
    let imgURL = NSURL(string: imgURLArray[indexPath.row])

    if imgURL != nil {
        print("check")
        let data = NSData(contentsOf: (imgURL as? URL)!)
        //cell.myImageView.image = UIImage(data: data as! Data)
        cell.myImageView.image = UIImage(data: data as! Data)

    }
    else{
        print("error")}
    //cell.myImageView.image = UIImage(named: array[indexPath.row] + ".JPG")
    return cell
}

/*func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
    //vc.imageString = imgURLArray[indexPath.row]
    //vc.nameString = nameArray[indexPath.row]
    //vc.dobString = dobArray[indexPath.row]

    //cell.myImageView.image = UIImage(named: array[indexPath.row] + ".JPG")
    //return cell1

    //self.navigationController?.pushViewController(vc, animated: true)    
}*/
}

【问题讨论】:

  • 您面临的问题是什么?
  • 我是 ios 的新手 .. 在我在上面的代码中创建的集合视图中单击项目后,我想转到另一个集合视图。
  • 请告诉我们您到底想要什么。
  • 如果你们能帮助我提供任何关于从集合视图导航到另一个不是详细视图而是另一个集合视图的视图的示例,我将不胜感激。@PGDev
  • 您可以从一个 UIViewController 导航到另一个 UIViewController。这就是你想要的吗?

标签: ios swift uinavigationcontroller uicollectionview


【解决方案1】:

以下是一个示例,说明如何实现您正在寻找的内容:

1.故事板

2。查看控制器

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        return collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {
        let controller = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
        self.navigationController?.pushViewController(controller, animated: true)
    }
}

class SecondViewController: UIViewController, UICollectionViewDataSource
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        return collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    }
}

如果您遇到任何问题,请回复。

【讨论】:

  • 你达到了你的要求吗?
  • 你能帮我举一个收集视图的例子吗?它从带有图像的 json 加载数据并单击视图中的项目以导航到具有再次从 json 加载数据的集合视图的另一个页面。 @PGDev
  • 是的,但我还不能导航到另一个集合视图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多