如何将不同的数据从表视图传递到另一个视图控制器

Posted

技术标签:

【中文标题】如何将不同的数据从表视图传递到另一个视图控制器【英文标题】:How to pass the different data from table view to another view controller 【发布时间】:2020-05-16 14:47:48 【问题描述】:

我想将数据从表视图控制器传递到集合视图。我想做如果我选择品牌A,它会显示A品牌,如果我选择品牌B,它会显示品牌B

目前,我已经为 Brand 2 设置了数组:“Acme De La Vie”,但它什么也没显示。谁能帮我??谢谢

这是我的代码:

ViewController 中的代码:

类:

 import UIKit
    class streetwearbrand
        var brandkeyword : String?
        var brandname : [String]?



    init(brandkeyword: String, brandname:[String]) 
        self.brandkeyword = brandkeyword
        self.brandname = brandname
    


class ViewController: UIViewController 

    @IBOutlet weak var SignUpButton: UIButton!
    @IBOutlet weak var LoginButton: UIButton!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var searchBar: UISearchBar!


var Streetwearbrand = [streetwearbrand]()

    var searchBrand = [String]()

    var searching = false

    override func viewDidLoad() 
        super.viewDidLoad()

        Streetwearbrand.append(streetwearbrand.init(brandkeyword: "A", brandname:["A Cold Wall", "Acme De La Vie", "Adidas", "Anti Social Social Club"]))

        Streetwearbrand.append(streetwearbrand.init(brandkeyword: "B", brandname:["Balenciaga", "Bape"]))

        Streetwearbrand.append(streetwearbrand.init(brandkeyword: "C", brandname:["CDG Play", "Champion"]))

        Streetwearbrand.append(streetwearbrand.init(brandkeyword: "E", brandname:["Essential"]))

        Streetwearbrand.append(streetwearbrand.init(brandkeyword: "F", brandname:["Fila"]))

        Streetwearbrand.append(streetwearbrand.init(brandkeyword: "K", brandname:["Kenzo", "Kith"]))

        Streetwearbrand.append(streetwearbrand.init(brandkeyword: "M", brandname:["Marcello Burlon", "Moschino"]))

        Streetwearbrand.append(streetwearbrand.init(brandkeyword: "N", brandname:["Nike"]))

        Streetwearbrand.append(streetwearbrand.init(brandkeyword: "O", brandname:["Obey", "Off White"]))
    


extension ViewController: UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate


    func numberOfSections(in tableView: UITableView) -> Int 

        return Streetwearbrand.count


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        if searching 
          return searchBrand.count
         else 
        return Streetwearbrand[section].brandname?.count ?? 0
    



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

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        if searching
            cell.textLabel?.text = searchBrand[indexPath.row]
         else cell.textLabel?.text = Streetwearbrand[indexPath.section].brandname?[indexPath.row]
    

    return cell


    //for title

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? 
        return Streetwearbrand[section].brandkeyword
    

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
        let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 40))
        view.backgroundColor = .lightGray
        let label = UILabel(frame: CGRect(x: 15, y: 0, width: view.frame.width - 15, height: 40))
        label.text = Streetwearbrand[section].brandkeyword
        view.addSubview(label)
        return view
    

    //to generate the height of the keyword label

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

To pass data from table view : 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
        let vc = storyboard?.instantiateViewController(withIdentifier: "SearchViewController") as? SearchViewController
        if Streetwearbrand[indexPath.item].brandname?[0] == "A Cold Wall"
        
            vc?.items.append(item(name: "Long Sleeve Abstract", price: "RM 1021", image: "longsleeve"))
            vc?.items.append(item(name: "Logo Graphic Print Top", price: "RM 860", image: "abstractprint"))
            vc?.items.append(item(name: "Material Study T-Shirt", price: "RM 750", image: "studymaterial"))
            vc?.items.append(item(name: "Mission Statement Print T-Shirt", price: "RM 750", image: "mission"))
            vc?.items.append(item(name: "Printed Logo T-Shirt", price: "RM 670", image: "white"))
            vc?.items.append(item(name: "Tie-Dye Print T-Shirt", price: "RM 760", image: "tiedye"))
         else if Streetwearbrand[indexPath.item].brandname?[1] == "Acme De La Vie"
        
            vc?.items.append(item(name: "ADLV Black Tee", price: "RM 250", image: "adlv2"))
            vc?.items.append(item(name: "ADLV X Sesame Street ", price: "RM 210", image: "adlv3"))
            vc?.items.append(item(name: "ADLV X Sesame Street", price: "RM 210", image: "adlv4"))
            vc?.items.append(item(name: "ADLV Black Hodie", price: "RM 300", image: "adlv5"))
            vc?.items.append(item(name: "ADLV X Sesame Street White", price: "RM 210", image: "adlv6"))
            vc?.items.append(item(name: "ADLV Black Tee", price: "RM 250", image: "adlv1"))
        

 self.navigationController?.pushViewController(vc!, animated: true)

    

Here my code at SearchViewController: 

import UIKit

here the struct 

struct item 
    var name : String
    var price : String
    var image : String


class SearchViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource 

var items = [item]()

    @IBOutlet weak var collectionView: UICollectionView!

    @IBOutlet weak var collectionviewflow: UICollectionViewFlowLayout!

    override func viewDidLoad() 
        super.viewDidLoad()

        collectionView.delegate = self
        collectionView.dataSource = self

    

    func numberOfSections(in collectionView: UICollectionView) -> Int 
        return 1
    

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
        let cellIndex = indexPath.item
        cell.imageView.image = UIImage(named: items[indexPath.item].image)
        cell.labelname.text = items[indexPath.item].name
        cell.labelprice.text = items[indexPath.item].price

        cell.contentView.layer.cornerRadius = 10
        cell.contentView.layer.borderWidth = 1.0
        cell.contentView.layer.borderColor = UIColor.gray.cgColor
        cell.backgroundColor = UIColor.white

        cell.layer.shadowColor = UIColor.gray.cgColor
        cell.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
        cell.layer.shadowRadius = 2.0
        cell.layer.shadowOpacity = 1.0
        cell.layer.masksToBounds = false
        cell.layer.shadowPath = UIBezierPath(roundedRect: cell.bounds, cornerRadius: cell.contentView.layer.cornerRadius).cgPath

        return cell
    

    override func viewWillDisappear(_ animated: Bool) 
        items.removeAll()
    



extension SearchViewController : UICollectionViewDelegateFlowLayout 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets 
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat 
        return 0
    

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat 
        return 10
    

【问题讨论】:

视图 A 和 B 是否嵌入在导航控制器中? 有2个视图控制器:视图控制器和searchviewcontroller @MAT 【参考方案1】:

要从表视图传递数据更新此方法,请改用节:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
        let vc = storyboard?.instantiateViewController(withIdentifier: "SearchViewController") as? SearchViewController
        if Streetwearbrand[indexPath.section].brandname?[indexPath.row] == "A Cold Wall"
        
            vc?.items.append(item(name: "Long Sleeve Abstract", price: "RM 1021", image: "longsleeve"))
            vc?.items.append(item(name: "Logo Graphic Print Top", price: "RM 860", image: "abstractprint"))
            vc?.items.append(item(name: "Material Study T-Shirt", price: "RM 750", image: "studymaterial"))
            vc?.items.append(item(name: "Mission Statement Print T-Shirt", price: "RM 750", image: "mission"))
            vc?.items.append(item(name: "Printed Logo T-Shirt", price: "RM 670", image: "white"))
            vc?.items.append(item(name: "Tie-Dye Print T-Shirt", price: "RM 760", image: "tiedye"))
         else if Streetwearbrand[indexPath.section].brandname?[indexpath.row] == "Acme De La Vie"
        
            vc?.items.append(item(name: "ADLV Black Tee", price: "RM 250", image: "adlv2"))
            vc?.items.append(item(name: "ADLV X Sesame Street ", price: "RM 210", image: "adlv3"))
            vc?.items.append(item(name: "ADLV X Sesame Street", price: "RM 210", image: "adlv4"))
            vc?.items.append(item(name: "ADLV Black Hodie", price: "RM 300", image: "adlv5"))
            vc?.items.append(item(name: "ADLV X Sesame Street White", price: "RM 210", image: "adlv6"))
            vc?.items.append(item(name: "ADLV Black Tee", price: "RM 250", image: "adlv1"))
        

 self.navigationController?.pushViewController(vc!, animated: true)

    

【讨论】:

我尝试使用该代码,但它不起作用,当我运行它时,当我按下“Acme De La Vie”时,它显示“A Cold Wall”产品。如果我按下“Acme De La Vie”,我想制作它,它会显示“Acme De la Vie”产品。你能再帮帮我吗? @jawadAli 它肯定会解决你的问题......以前......你的第一次检查总是正确的......这就是为什么你总是看到“冷墙”

以上是关于如何将不同的数据从表视图传递到另一个视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式将数据从表视图控制器传递到另一个控制器? [复制]

将标签数据从表视图传递到另一个视图控制器

如何将核心数据信息从表视图传递到视图控制器?

如何将标签中的数据从表视图行操作发送到视图控制器?

将数据从 viewcontroller tableview 传递到另一个视图控制器

从表视图到另一个表视图之间传递数据(多)[重复]