gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 05:37:41 25 4
gpt4 key购买 nike

请耐心等待,因为我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com