使用选取器视图过滤CollectionView

时间:2018-07-10 18:23:02

标签: ios swift uicollectionview uipickerview

现在,我在代码中使用UICollectionView扩展名来显示我餐厅的所有菜单项。我想将此连接到View控制器中的选择器视图,以根据产品类型过滤商品。到目前为止,这是我的代码:

收藏夹视图

   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "productCells", for: indexPath) as! ProductCollectionCell

        let product = products[indexPath.item]


        }

        cell.burgerName.text = product.name?.capitalized
        cell.desc.text = product.size?.capitalized
        cell.desc.textColor = UIColor.white


        if let image = product.image {
            Helpers.loadImage(cell.productImage, "\(image)")
        }

        cell.layer.borderColor = UIColor.lightGray.cgColor
        cell.layer.borderWidth = 0.5

        return cell
    }

选择器视图

  func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let view = UIView()
        view.frame = CGRect(x: 0, y: 50, width: width, height: 40)
//        view.backgroundColor = UIColor.darkGray
//        view.isOpaque = true

        let label = UILabel()
        label.frame = CGRect(x: 0, y: 15, width: width, height: 26)
        label.textAlignment = .center

        label.font = UIFont.systemFont(ofSize: 10)
        label.text = sections[row]
        label.textColor = UIColor.purple
        label.highlightedTextColor = UIColor.darkText
        view.addSubview(label)

        // view rotation
        view.transform = CGAffineTransform(rotationAngle: 90 * (.pi/180))

        return view
    }


    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        filteredProducts = self.products.filter({ (prod: Product) -> Bool in
            return prod.filter?.lowercased().range(of: UICollectionView())
        })

    }

我不确定如何根据变量Section进行过滤。这是products[indexPath.item]的另一个变量,称为Type。因此,当PickerView落在该类型上时,我希望collectionview仅显示具有该TYPE的项目。

任何帮助将不胜感激。

0 个答案:

没有答案