2

Here is my code snippet:

class ProductCategoryCell: UITableViewCell {

  @IBOutlet weak var collectionViewProducts: UICollectionView!

  // other stuff...

  func setProducts() {
    let productsObservable = Observable.just([
      Product(name: "test", price: 10.0),
      Product(name: "test", price: 10.0),
      Product(name: "test", price: 10.0)
    ])

    productsObservable.bindTo(collectionViewProducts.rx.items(cellIdentifier: "ProductCell", cellType: ProductCell.self)) {
      (row, element, cell) in
      cell.setProduct(element)
    }.disposed(by: disposeBag)
  }
}

It is giving me a build error:

No 'items' candidates produce the expected contextual result type '(Observable<[Product]>) -> (_) -> _'

In my view controller, I am populating table view with a similar code:

let productsObservable = Observable.just(testProducts)
productsObservable.bindTo(tableViewProducts.rx.items(cellIdentifier: "ProductCategoryCell", cellType: ProductCategoryCell.self)) { (row, element, cell) in
        cell.setCategory(category: element)
}.disposed(by: disposeBag)

This code works normally for. What am I doing wrong?

Gasim
  • 7,615
  • 14
  • 64
  • 131

1 Answers1

5

I was able to reproduce the error message you are getting. You did not post the code for your ProductCell's setProduct() method but I guess you might have the same problem that I had.

This is my dummy ProductCell implementation:

class ProductCell: UICollectionViewCell {
    func setProduct(product: Product) {
        // do stuff
    }
}

Now when I use the code in your answer:

productsObservable.bindTo(collectionViewProducts.rx.items(cellIdentifier: "ProductCell", cellType: ProductCell.self)) { (row, element, cell) in
   cell.setProduct(element)
}
.disposed(by: disposeBag)

I get the same error than you:

No 'items' candidates produce the expected contextual result type '(Observable<[Product]>) -> (_) -> _'

In my case the problem was that I forgot the argument label product when calling setProduct:

After adding the argument label the code compiles without error:

productsObservable.bindTo(collectionViewProducts.rx.items(cellIdentifier: "ProductCell", cellType: ProductCell.self)) { (row, element, cell) in
   cell.setProduct(product: element)
}
.disposed(by: disposeBag)

The compiler's error message is very misleading in this case. When you would try to call setProduct(element) outside of the closure you would get the correct error message:

Missing argument label 'product:' in call

But somehow the compiler does not realize what exactly the problem is when he is inside the closure.

As I mentioned before I don't know how you implemented setProduct in your ProductCell, but because you are calling cell.setCategory(category: element) in your UITableView example I assume that your problem is the missing argument label when you call cell.setProduct(element)

joern
  • 27,354
  • 7
  • 90
  • 105