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

时间:2020-05-16 14:47:48

标签: ios swift xcode uitableview

我想将数据从表视图控制器传递到集合视图。我要选择“品牌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
    }
}

1 个答案:

答案 0 :(得分:0)

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

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)

    }

我希望它将解决此问题...编码愉快=)

相关问题