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

用户名

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

现在,我已经为品牌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
    }
}
贾瓦德·阿里

要从表视图传递数据,请更新此方法。

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)

    }

我希望它能解决问题...快乐编码=)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将数据从一个视图控制器传递到另一个视图控制器,并且在视图控制器之间有一个显示视图控制器

来自分类Dev

如何将数据从一个视图控制器传递到另一个SWIFT

来自分类Dev

如何将数据从一个视图控制器传递到另一个SWIFT

来自分类Dev

如何将数据从一个视图控制器传递到另一个?

来自分类Dev

如何将 NSMutableDictionary 从一个视图控制器传递到另一个视图控制器?

来自分类Dev

如何将信息从一个视图控制器中的表视图单元传递到另一视图控制器?

来自分类Dev

如何将数据从一个视图控制器传递到导航控制器中嵌入的另一个视图控制器

来自分类Dev

iOS-将数据传递到另一个视图控制器

来自分类Dev

如何将数组快速传递给另一个视图控制器?

来自分类Dev

如何将数组快速传递给另一个视图控制器?

来自分类Dev

如何将标签数据传递到第二个视图控制器中的另一个标签

来自分类Dev

如何将 varbinary(max) 值作为参数从视图传递到控制器中的 actionmethod 并将其保存到另一个表

来自分类Dev

iOS如何将数组隔离到另一个视图控制器

来自分类Dev

将数据传递到导航控制器中的另一个视图控制器

来自分类Dev

实例化另一个视图控制器时如何传递数据

来自分类Dev

通过 Segue 将对象从表视图单元格传递到另一个视图控制器

来自分类Dev

表视图单元格到另一个视图控制器

来自分类Dev

如何将按钮从一个视图控制器隐藏到另一个

来自分类Dev

如何从另一个视图控制器iOS刷新表视图?

来自分类Dev

使用link_to将数据从视图传递到另一个控制器-Ruby on Rails

来自分类Dev

准备segue时无法将数据传递到另一个视图控制器

来自分类Dev

将 JSON 数据从 HTTP 请求传递到 Swift 3 中的另一个视图控制器

来自分类Dev

将变量从页面视图控制器传递到另一个

来自分类Dev

在ngRoute控制器之间传递数据的最佳方法(从一个视图到另一个视图)

来自分类Dev

将数据从视图传递给控制器,其中数据来自另一个控制器

来自分类Dev

无法从另一个视图控制器获取一个表视图数据

来自分类Dev

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

来自分类Dev

Ruby on Rails:如何使用不同的控制器将一个视图/页面链接/路由到另一个视图/页面

来自分类Dev

从AngularJS如何将数据从一个控制器+模板传递到另一个控制器+模板?

Related 相关文章

  1. 1

    如何将数据从一个视图控制器传递到另一个视图控制器,并且在视图控制器之间有一个显示视图控制器

  2. 2

    如何将数据从一个视图控制器传递到另一个SWIFT

  3. 3

    如何将数据从一个视图控制器传递到另一个SWIFT

  4. 4

    如何将数据从一个视图控制器传递到另一个?

  5. 5

    如何将 NSMutableDictionary 从一个视图控制器传递到另一个视图控制器?

  6. 6

    如何将信息从一个视图控制器中的表视图单元传递到另一视图控制器?

  7. 7

    如何将数据从一个视图控制器传递到导航控制器中嵌入的另一个视图控制器

  8. 8

    iOS-将数据传递到另一个视图控制器

  9. 9

    如何将数组快速传递给另一个视图控制器?

  10. 10

    如何将数组快速传递给另一个视图控制器?

  11. 11

    如何将标签数据传递到第二个视图控制器中的另一个标签

  12. 12

    如何将 varbinary(max) 值作为参数从视图传递到控制器中的 actionmethod 并将其保存到另一个表

  13. 13

    iOS如何将数组隔离到另一个视图控制器

  14. 14

    将数据传递到导航控制器中的另一个视图控制器

  15. 15

    实例化另一个视图控制器时如何传递数据

  16. 16

    通过 Segue 将对象从表视图单元格传递到另一个视图控制器

  17. 17

    表视图单元格到另一个视图控制器

  18. 18

    如何将按钮从一个视图控制器隐藏到另一个

  19. 19

    如何从另一个视图控制器iOS刷新表视图?

  20. 20

    使用link_to将数据从视图传递到另一个控制器-Ruby on Rails

  21. 21

    准备segue时无法将数据传递到另一个视图控制器

  22. 22

    将 JSON 数据从 HTTP 请求传递到 Swift 3 中的另一个视图控制器

  23. 23

    将变量从页面视图控制器传递到另一个

  24. 24

    在ngRoute控制器之间传递数据的最佳方法(从一个视图到另一个视图)

  25. 25

    将数据从视图传递给控制器,其中数据来自另一个控制器

  26. 26

    无法从另一个视图控制器获取一个表视图数据

  27. 27

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

  28. 28

    Ruby on Rails:如何使用不同的控制器将一个视图/页面链接/路由到另一个视图/页面

  29. 29

    从AngularJS如何将数据从一个控制器+模板传递到另一个控制器+模板?

热门标签

归档