【问题标题】:Passing information from a collection view to a tableview将信息从集合视图传递到表视图
【发布时间】:2020-09-21 13:33:28
【问题描述】:

现在我有一个表格视图,它是集合视图的扩展。

这是故事板的图片。

该应用程序的主要前提类似于 Apple Maps。我希望选择一个 collectionView 单元格,然后让 viewController 显示一个 tableView,其中包含该类别中所有受尊重的项目。

这是我的 collectionView 代码。

 class ContentViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

let imageArray = [UIImage(named: "image 1"), UIImage(named: "image 2"), UIImage(named: "image 3")]

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
    cell.mapIconImage.image = imageArray[indexPath.row]
    cell.mapIconLabel.text! = imageNameArray[indexPath.row]
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    performSegue(withIdentifier: "ShowTableViewPlaces", sender: self)
}

class CustomCell: UICollectionViewCell {

@IBOutlet weak var mapIconImage: UIImageView!
@IBOutlet weak var mapIconLabel: UILabel!

}

这是目前为止 tableView 的代码。

import UIKit

struct PlacesInTableView {
var name: String

init(name: String) {
    self.name = name
}
}

class MapItemsTableViewController: UITableViewController {

var image1InTableView = [PlacesInTableView(name: "place 1"),
    PlacesInTableView(name: "place 2"),
    PlacesInTableView(name: "place 3")
]

var image2InTableView = [PlacesInTableView(name: "place 1")
]

var image3InTableView = [PlacesInTableView(name: "place 1"),
    PlacesInTableView(name: "place 2")
]

选择收集视图时,我希望在该尊重的类别中的所有位置占据TableView的内容。我将如何将数据从集合视图单元格传递到新的 tableView?

【问题讨论】:

  • 目前还不清楚该类别中受尊重的项目与集合视图的数据源数组有何关联。该代码包含一组图像和一个未知的imageNameArray。顺便说一句,使用多个数组作为数据源是非常糟糕的做法,强烈建议不要这样做。
  • 您对问题的解释不清楚。例如,“显示一个包含该类别中所有受尊重项目的 tableView”。 “类别”是什么意思?您只提到了集合视图。这与类别有什么关系?您是什么意思“受人尊敬的物品”(来自类别)?你能举一个类别和“受人尊敬的项目”的例子吗?

标签: ios swift xcode uitableview uicollectionview


【解决方案1】:

为表 vc "MapItemsTableViewController" 添加情节提要 id 并在 ContentViewController 中添加以下代码

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let vc = self.storyboard?.instantiateViewController(withIdentifier: "MapItemsTableViewController") as? MapItemsTableViewController {
            vc.imageCate = indexPath.row
            self.present(vc, animated: true, completion: nil)
        }
}

然后你可以为 MapItemsTableViewController 实现如下

class MapItemsTableViewController: UITableViewController {
    var imageCate:Int?
    var image1InTableView = [PlacesInTableView(name: "place 1"),
                             PlacesInTableView(name: "place 2"),
                             PlacesInTableView(name: "place 3")
    ]

    var image2InTableView = [PlacesInTableView(name: "place 1")
    ]

    var image3InTableView = [PlacesInTableView(name: "place 1"),
                             PlacesInTableView(name: "place 2")
    ]

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if imageCate == 0 {
           return image1InTableView.count
        }
        else if imageCate == 1{
            return image2InTableView.count
        }else if imageCate == 2{
            return image3InTableView.count
        }else{
            return 0
        }
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()

        if imageCate == 0 {
            cell.textLabel?.text = image1InTableView[indexPath.row].name
        }
        else if imageCate == 1{
            cell.textLabel?.text = image2InTableView[indexPath.row].name
        }else if imageCate == 2{
            cell.textLabel?.text = image3InTableView[indexPath.row].name
        }else{

        }
        return cell
    }

}

【讨论】:

    【解决方案2】:

    据我了解,您想将 name 值传递给 MapItemsTableViewController 吗?

    您可以覆盖准备功能:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
        let viewController = segue.destination as! MapItemsTableViewController
    
        viewController.name = // Data can input here
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多