【问题标题】:Collection View Cell selection using swift使用 swift 选择集合视图单元格
【发布时间】:2016-05-01 07:24:04
【问题描述】:

请多多包涵,因为我是 swift 新手。 我正在尝试创建纽约市 10 家餐厅的收藏视图。当用户单击餐厅(包含餐厅图像的单元格)时,我希望弹出一个新的集合视图控制器,其中包含餐厅菜单的集合视图。

我希望能够使用一个集合视图控制器在用户点击餐厅(一个单元格)时显示每个餐厅的不同菜单。

我正在使用一个原型单元来填充所有 10 家餐厅。我现在的困难是如何让每个单元格弹出菜单集合视图控制器,其中包含所选特定餐厅的菜单集。菜单集合视图控制器将显示食物、名称和价格的图像(几乎就像您选择商店时 Instacart 显示杂货的方式一样。现在,我只能点击任何单元格并出现相同的菜单。

这是我的餐厅列表集合视图控制器。已评论的 DidSelect 是我试图根据它们的索引路径选择单元格。

    import UIKit

    private let reuseIdentifier = "ManhattanCell"

    class ManhattanCollectionViewController: UICollectionViewController {

var yourRest = [String]()


override func viewDidLoad() {
    super.viewDidLoad()
    yourRest = ["RestAwash",
        "RestZoma",
        "RestLeBaobab",
        "RestSokhna",
        "RestCoumba",
        "RestMassawa",
        "RestLesAmbassades",
        "RestPonti",
        "RestBraai",
        "RestNewIvoire"]
}

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return yourRest.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! RestaurantCollectionViewCell

    // Configure the cell

    let image = UIImage(named: yourRest[indexPath.row])
    cell.manhattanImageView.image = image

    return cell
}

   /* override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row == 0 {
        // call your alert here
        self.performSegueWithIdentifier("awashShowDetail", sender: self)

    } else if indexPath.row == 1 {
        self.performSegueWithIdentifier("testView", sender: self)
    }
}
    */




    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    let sideSize = collectionView.frame.size.width / 2.51;
    return CGSize(width: sideSize, height: sideSize);
}
    }

下面是我的 RestaurantCollectionViewCell

     import UIKit

    class RestaurantCollectionViewCell: UICollectionViewCell {

             @IBOutlet weak var manhattanImageView: UIImageView!
    }

我的问题似乎有点类似于这个问题。我按照答案进行操作,但仍然无法正常工作。

Swift: UICollectionView selecting cell indexPath issues

谢谢。

【问题讨论】:

  • 如何将单元格映射到不同的 segue?
  • 我只有一个原型单元,我将它连接到 MenuCollectionViewController。还是我必须为每个餐厅名称创建 10 个不同的单元格,然后将每个单元格连接到 MenuCollectionViewController 以显示每个餐厅的菜单?
  • 这取决于你想要什么。如果每个菜单集合都有不同的外观,那么您将需要为每个集合创建一个自定义视图控制器。如果你能想出一种方法来为每个菜单集合动态交换数据,那么你只需要一个视图控制器。

标签: ios swift uicollectionview ios9 uicollectionviewcell


【解决方案1】:

我将继续假设您将有 10 个菜单集合视图控制器。这个建议适用于只有一个视图控制器,除了 segueIdentifier 之外,将有一个菜单控制器数据属性。


我建议创建一个数据模型来保存 Rest 信息。

struct MyRest {
    let imageName: String
    let segueIdentifier: String
}

将您的 yourRest 属性转换为 MyRest 对象数组。

var yourRestX = [MyRest]()

然后在viewDidLoad()

yourRest = [
    MyRest(imageName: "RestAwash", segueIdentifier: "awashShowDetail"),
    MyRest(imageName: "RestZoma", segueIdentifier: "zomaShowDetail"),
    MyRest(imageName: "RestLeBaobab", segueIdentifier: "leBaobabShowDetail"),
    MyRest(imageName: "RestSokhna", segueIdentifier: "sokhnaShowDetail"),
    MyRest(imageName: "RestCoumba", segueIdentifier: "coumbaShowDetail"),
    MyRest(imageName: "RestMassawa", segueIdentifier: "massawaShowDetail"),
    MyRest(imageName: "RestLesAmbassades", segueIdentifier: "lesAmbassadesShowDetail"),
    MyRest(imageName: "RestPonti", segueIdentifier: "rontiShowDetail"),
    MyRest(imageName: "RestBraai", segueIdentifier: "braaiShowDetail"),
    MyRest(imageName: "RestNewIvoire", segueIdentifier: "newIvoireShowDetail"),
]

当然,这意味着您将需要 10 个菜单集合视图控制器,每个控制器都按所列名称命名。 (不是一个很好的解决方案,但这完全取决于您的需求)。

collectionView(,cellForItemAtIndexPath)

// Configure the cell
let image = UIImage(named: yourRest[indexPath.row].imageName)
cell.manhattanImageView.image = image

然后在collectionView(,didSelectItemAtIndexPath)

let identifier = yourRest[indexPath.row].segueIdentifier
self.performSegueWithIdentifier(identifier, sender: self)

【讨论】:

  • 谢谢杰夫。我尝试了您的解决方案并且它有效。我现在面临的唯一问题是,我有 10 个菜单集合视图控制器,而故事板只允许我从 RestaurantCollectionViewCell 到 MenuCollectionView 有一个 segue。我创建了 10 个原型单元并连接到 10 个 MenuCollectionViewController 中的每一个。这很好,除了我有一个错误,“多个原型单元格都有标识符:”ManhattanCell”。我为每个单元格更改了,我只能使用一个。这是我上面代码中的reuseidzntifier。你觉得我怎么样这里做错了吗?
猜你喜欢
  • 1970-01-01
  • 2023-03-19
  • 2023-03-19
  • 2020-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多