sap-tutorials / Tutorials

Tutorials on sap.com
https://developers.sap.com/tutorial-navigator.html
Creative Commons Attribution 4.0 International
720 stars 773 forks source link

Build a Product List #5650

Closed jastill closed 4 years ago

jastill commented 4 years ago

Tutorial URL: https://developers.sap.com/tutorials/fiori-ios-scpms-starter-mission-03.html

Please specify the step you are referring to Step 4

The searchedProducts are not bound to the table data delegate. As such the search completes, but is never reflected in the UI.

Maybe I missed something, but these are the changes I made to get it working:

I replaced products with searchedProducts in the following

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //return products.count
    return searchedProducts.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //let product = products[indexPath.row]
    let product = searchedProducts[indexPath.row]
    let productCell = tableView.dequeueReusableCell(withIdentifier: FUIObjectTableViewCell.reuseIdentifier) as! FUIObjectTableViewCell
    productCell.accessoryType = .detailDisclosureButton
    productCell.headlineText = product.name ?? "-"
    productCell.subheadlineText = product.categoryName ?? "-"
    productCell.footnoteText = product.stockDetails?.quantity?.intValue() != 0 ? NSLocalizedString("In Stock", comment: "") : NSLocalizedString("Out", comment: "")
    // set a placeholder image
    productCell.detailImageView.image = FUIIconLibrary.system.imageLibrary

    // This URL is found in Mobile Services
    let baseURL = "https://i804190trial-dev-com-example-tutorialapp.cfapps.us10.hana.ondemand.com/SampleServices/ESPM.svc/v2"
    let url = URL(string: baseURL.appending(productImageURLs[indexPath.row]))

    guard let unwrapped = url else {
        logger.info("URL for product image is nil. Returning cell without image.")
        return productCell
    }
    // check if the image is already in the cache
    if let img = imageCache[unwrapped.absoluteString] {
        productCell.detailImageView.image = img
    } else {
        // The image is not cached yet, so download it.
        loadImageFrom(unwrapped) { image in
            productCell.detailImageView.image = image
        }
    }
    // Only visible on regular
    productCell.descriptionText = product.longDescription ?? ""

    return productCell
}

and

// actual search logic for finding the correct products for the term the user is searching for
private func searchProducts(_ searchText: String) {
    if !searchTextIsEmpty() {
        searchedProducts = products.filter({( product : Product) -> Bool in
            // Make sure the string is completely lower-cased or upper-cased. Either way makes it easier for you to
            // compare strings.
            return product.name?.lowercased().contains(searchText.lowercased()) ?? false
        })

        // Don't forget to trigger a reload.
        tableView.reloadData()
    }
}

private func loadData() {
    showFioriLoadingIndicator()
    fetchProducts {
        self.searchedProducts = self.products
        self.tableView.reloadData()
        self.hideFioriLoadingIndicator()
    }
}
jastill commented 4 years ago

Now I am on mission3 i see what was missed in cellForRowAtIndexPath and numberOfRowsInSection:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return isSearching() ? searchedProducts.count : products.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let product = isSearching() ? searchedProducts[indexPath.row] : products[indexPath.row]
thecodester commented 4 years ago

Can we close this issue?

KevinRiedelsheimer commented 4 years ago

Hi @jastill ,

Thanks for reaching out.

Would it make it easier for you if I would change the order or add a statement saying that additional implementation is needed to make it fully functional?

Thanks, Kevin