ios - 使用 swift 进行 Collection View Cell 选择

标签 ios swift uicollectionview ios9 uicollectionviewcell

请耐心等待,因为我是 swift 的新手。 我正在尝试创建纽约市 10 家餐厅的收藏 View 。当用户单击餐厅(包含餐厅图像的单元格)时,我希望弹出一个新的 Collection View Controller ,其中包含餐厅菜单的 Collection View 。

我希望能够使用一个 Collection View Controller 来显示当用户点击餐厅(一个 Cell)时每家餐厅的不同菜单。

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

这是我的餐厅列表 Collection View Controller 。被评论的 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

谢谢。

最佳答案

我将继续假设您将有 10 个菜单 Collection View Controller 。此建议适用于只有一个 View Controller ,除了 segueIdentifier 之外,将有一个菜单 Controller 数据属性。


我建议创建一个数据模型来保存 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 个菜单 Collection View Controller ,每个都按所列名称命名。 (这不是一个很好的解决方案,但这完全取决于您的需求)。

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)

关于ios - 使用 swift 进行 Collection View Cell 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34984909/

相关文章:

ios - 将 ImageView 放入 Collection View 单元格导致 IBTool 发出警告

objective-c - Xcode 4 和分支

ios - 如何推迟【权限请求提醒】?

c++ - 用 Objective-C++ 包装 C++ 代码

ios - UIViewController 中的 Collection View 可选展开崩溃

ios - CollectionView didSelectItemAtIndexPath 不起作用 - Swift

ios - fatal error : unexpectedly found nil while unwrapping an Optional values

javascript - 如何将值从 javascript 传递到 iOS UIWebView

ios - S3GetObjectMetadataResponse 错误,崩溃。我怎样才能检查它?

ios - 如何将数据附加到来自 swift 3 中服务器响应的字典中?