我正在使用集合视图来显示自定义集合视图单元格的网格,以表示类别图标,这些图标将用作按钮以连接到表视图控制器以显示类别列表。我正在使用一系列"产品"(标签)和"图像"(产品图像)来显示基于原型的多个自定义单元格
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell
cell.imageView?.image = self.images[indexPath.row]
cell.labelView?.text = self.products[indexPath.row]
return cell
}
如何将我的主页视图控制器连接到单个自定义表视图控制器,以根据选择的图标显示不同的类别列表?非常感谢任何帮助,仍然习惯于xcode和swift
理解正确,您可以使用同一个子 UITableViewController 并根据选择的单元格向其传递不同的数据。
在集合视图控制器实现中的第一个
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
然后对数据执行 segue
performSegue("identifier", sender: self.products[indexPath.row])
首先,视图控制器必须符合UICollectionViewDelegate
然后您必须实现此委托方法以在选择单元格时执行 segue:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("identifier", sender: nil)
}
然后检查 segue 标识符是否匹配,获取所选单元格的indexPath
,构建所选产品和图像的元组,最后将其传递给您的目标tableViewController
。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "identifier" {
let indexPath = collectionView.indexPathsForSelectedItems()!.first!
let selectedProduct = (product: products[indexPath.row], image: images[indexPath.row])
let controller = segue.destinationViewController as! YourTableViewController
controller.product = selectedProduct
}
}