如何在自定义表格视图单元格之间传递数据?

时间:2019-02-25 18:28:49

标签: ios swift tableview custom-cell

我有两个具有自定义功能的自定义表视图(不是表视图控制器)

个单元格。我想从 leaflet(c) %>% addTiles() %>% addCircleMarkers(lng=c$lon, lat=c$lat, color = ~pal(decile)) %>% addLegend("bottomright", pal = pal, values = ~decile, title = "Deciles")

中选择另一个单元格

并选择

单元格信息的标题和价格以

的形式传递到第二个视图

DetailViewController的第一个和第二个索引标签。

这是我的MainViewController.swift

编辑:这是答案

MainViewcontroller

}

// MARK:-SearchBar数据 扩展MainViewController:UISearchBarDelegate {

import UIKit

class MainViewController: UIViewController, UITableViewDataSource,      UITableViewDelegate,
UICollectionViewDelegate, UICollectionViewDataSource,    UICollectionViewDelegateFlowLayout {

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var mainTableView: UITableView!

var imageNames = [ImageNames]()
var searchFoods: [String]!
var priceFood: [Double]!
var searching = false


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.tabBarController?.tabBar.isHidden = false
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.navigationBar.isHidden = true
    let foodCell = Food(name: ["Hamburger big mac",
                               "Patates",
                               "Whopper",
                               "Steakhouse"], price: [15.0, 20.0, 25.0, 30.0])

   searchBar.delegate = self
   searchFoods = foodCell.name
   priceFood = foodCell.price



    imageNames = [
        ImageNames(name: "images"),
        ImageNames(name: "unnamed"),
        ImageNames(name: "unnamed")
    ]

}

func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return section == 0 ? 1 : searchFoods.count
    //        return section == 0 ? 1 : foodNames.count

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return indexPath.section == 0 ? 130 : 65
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return indexPath.section == 0 ? 100 : 65
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MainFoodTableViewCell", for: indexPath) as! MainFoodTableViewCell

        cell.mainFoodCollectionView.delegate = self
        cell.mainFoodCollectionView.dataSource = self
        cell.mainFoodCollectionView.reloadData()
        cell.mainFoodCollectionView.tag = indexPath.row
        return cell

    } else {

        let cell = tableView.dequeueReusableCell(withIdentifier: "CellForFood", for: indexPath) as! MainFoodTitleTableViewCell

        cell.titleLabel?.text = searchFoods[indexPath.row]
        cell.priceLabel?.text = priceFood[indexPath.row].description

        return cell
    }

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "cellForFoodSegue" {
        if let destinationViewController = segue.destination as? DetailViewController
        {
            let indexPath = self.mainTableView.indexPathForSelectedRow!

            var foodNameArray: [String]
            var foodPriceArray: [Double]

            foodNameArray = [searchFoods[indexPath.row]]
            foodPriceArray = [priceFood[indexPath.row]]

            destinationViewController.detailFoodName = foodNameArray
            destinationViewController.detailFoodPrice = foodPriceArray

        }

    }
}

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

//MARK:- collection view cell size
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = UIScreen.main.bounds.width
    return CGSize(width: width, height: 130)
}

//MARK:- //collection view cell data
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainFoodCollectionViewCell", for: indexPath) as! MainFoodCollectionViewCell
    let img = imageNames[indexPath.row]
    cell.mainFoodImage.image = UIImage(named: img.name)
    return cell
}

编辑:这是我的DetailViewController

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    self.searchBar.showsCancelButton = true
    mainTableView.reloadData()
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    mainTableView.reloadData()
    searchBar.showsCancelButton = false
    searchBar.text = ""
    searchBar.resignFirstResponder()

}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    searchFoods = searchText.isEmpty ? searchFoods : searchFoods.filter { (item: String) -> Bool in
        return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
    }

    mainTableView.reloadData()
}
}

1 个答案:

答案 0 :(得分:0)

您不是在寻找prepare(for segue: sender:)方法吗?通常,您需要使用segue将单元原型绑定到详细信息视图,并且在选择单元格之后,将在调用该方法以准备将数据注入到详细信息视图之后遵循segue。