具有相同 XIB 单元格和按钮的多个集合视图

Posted

技术标签:

【中文标题】具有相同 XIB 单元格和按钮的多个集合视图【英文标题】:Multiple Collection Views with same XIB Cell and button 【发布时间】:2017-01-30 12:19:03 【问题描述】:

我有一个包含多个集合视图的视图控制器。

每个集合视图都使用与 xib 相同的自定义单元格。在这个 xib 中,我有一个按钮。

collectionViews 的名字是

1) manPerfumeCV
2) womanPerfumeCV
3) kidsPerfumeCV

cellForItemAt 里面我有cell.productCart.tag = indexPath.row

    let cell:iPhoneCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "iPhoneCell", for: indexPath) as! iPhoneCollectionViewCell
    if collectionView == self.womanPerfumeCV 

        let prods = womanPerfumeData[indexPath.row]
        cell.configureCell(products: prods)
        cell.productCart.tag = indexPath.row

     else if collectionView == self.manPerfumeCV 
        let prods = manPerfumeData[indexPath.row]
        cell.configureCell(products: prods)
        cell.productCart.tag = indexPath.row

     else if collectionView == self.kidsPerfumeCV 
        let prods = kidsPerfumeData[indexPath.row]
        cell.configureCell(products: prods)
        cell.productCart.tag = indexPath.row

    

在同一个视图控制器中,我对 xib 文件中的按钮执行此操作

@IBAction func iPhoneAddToCart(_ sender: AnyObject) 
        let butt = sender as! UIButton
        let indexP = IndexPath(row: butt.tag, section: 0)
        let cell = manPerfumeCV.cellForItem(at: indexP) as! iPhoneCollectionViewCell

        print(manPerfumeData[indexP.row].price)

    

每个集合视图都有自己的 [Products] 数组。

1) manPerfumeData
2) womanPerfumeData
3) kidPerfumeData.

在我的代码中,如果我用 manPerfumeData 点击位于第一个集合视图中的按钮,它会很好地打印价格。

虽然如果我按下第二个或第三个集合视图上的按钮,应用程序就会崩溃。

有什么方法可以从他按下按钮的收藏视图中知道,以便我可以加载特定的 [Products] 数组??

谢谢!

【问题讨论】:

您在调试器输出中看到了什么? @SinaKH 致命错误:在展开可选值时意外发现 nil 2017-01-30 14:25:38.928171 Aromania[19538:6823926] 致命错误:在展开可选值时意外发现 nil 这是因为在print 我只有manPerfumeData。我如何识别在哪个集合视图中按下了按钮? 【参考方案1】:

您可以使用UIButtontag 属性。 让我们考虑您的代码

let cell:iPhoneCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "iPhoneCell", for: indexPath) as! iPhoneCollectionViewCell
if collectionView == self.womanPerfumeCV 
    //Your Code
    cell.productCart.tag = indexPath.row + 1000

 else if collectionView == self.manPerfumeCV 
    //Your Code
    cell.productCart.tag = indexPath.row + 2000

 else if collectionView == self.kidsPerfumeCV 
    //Your Code
    cell.productCart.tag = indexPath.row + 3000

您注意到我更改了UIButtontag 属性。现在当点击按钮时,检查UIButtontag 属性。像这样

if (sender.tag >= 1000 && sender.tag<2000)

  //manPerfumeCV

else if (sender.tag >= 2000 && sender.tag<3000)

  //womanPerfumeCV

else

  //kidsPerfumeCV

为了从数组中获取值,从tag属性中减去开始值,你会得到值,像这样

对于ma​​nPerfumeCV

indexValue = sender.tag - 1000

对于womanPerfumeCV

indexValue = sender.tag - 2000

对于kidsPerfumeCV

indexValue = sender.tag - 3000

这个值可以直接用于从数组中获取数据。

【讨论】:

是的,它是正确的!!!我想出了你所做的最后一次更新,我必须制作- 1000 或者无论如何我在每个标签上添加的数字!非常感谢队友:D【参考方案2】:

为了实现它,你可以在 viewDidLoad 中的 collectionViews 中添加 tag 属性

override func viewDidLoad() 

// your code

self.womanPerfumeCV.tag = 0;
self.manPerfumeCV.tag = 1;
self.kidsPerfumeCV = 2;


使用它您可以识别来源

//现在是时候收听buttonEvent了

我建议将事件监听器保留在同一个控制器中,或者只使用索引和标签进行协议回调。 为了简单起见,让我们在同一个 viewController 文件中进行更改

if collectionView == self.womanPerfumeCV 

    let prods = womanPerfumeData[indexPath.row]
    cell.configureCell(products: prods)
    cell.productCart.tag = indexPath.row

 else if collectionView == self.manPerfumeCV 
    let prods = manPerfumeData[indexPath.row]
    cell.configureCell(products: prods)
    cell.productCart.tag = indexPath.row

 else if collectionView == self.kidsPerfumeCV 
    let prods = kidsPerfumeData[indexPath.row]
    cell.configureCell(products: prods)
    cell.productCart.tag = indexPath.row



cell.yourButton.tag = (collectionView.tag * 1000 )+(indexPath.row);
        cell.yourButton.addTarget(self, action: #selector(ButtonClicked(_:)), forControlEvents: .TouchUpInside)

现在向事件添加选择器

func ButtonClicked(sender: UIButton) 
    let index : Int = sender.tag % 1000;
    switch sender.tag 
      case let x where x < 1000:
      loadDataFromFirstArrayWithIndex(index);break;
      case let x where x <2000:
      loadDataFromSecondArrayWithIndex(index);break;
      default:
      loadDataFromThirdArrayWithIndex(index);

     

【讨论】:

以上是关于具有相同 XIB 单元格和按钮的多个集合视图的主要内容,如果未能解决你的问题,请参考以下文章

如何在同一个xib中放置自定义单元格和表格视图?

表视图单元格和表视图标题视图的大小

在集合视图中有包含数据的单元格和空单元格

在集合单元格中添加背景视图

NSTableView,多个单元格和绑定

如何使用集合视图单元进行下一个视图?