【问题标题】:how to extract json array and use it without tableview如何提取json数组并在没有tableview的情况下使用它
【发布时间】:2017-01-05 07:15:45
【问题描述】:

假设我收到了这样的 JSON 响应:

{
"product_list" =     (
            {
        "product_id" = 33;
        "product_image" =             (
        );
        "product_sku" = asdasd;
        "product_title" = "Product 1";
    },
            {
        "product_id" = 58;
        "product_image" =             (
        );
        "product_sku" = kkkk222;
        "product_title" = p1;
    },
            {
        "product_id" = 119;
        "product_image" =             (
        );
        "product_sku" = 123456;
        "product_title" = product;
    },
            {
        "product_id" = 120;
        "product_image" =             (
        );
        "product_sku" = 456789;
        "product_title" = "product 3";
    }
);
  "xxx_api" = "product_information";
}

这就是我使用 cellForRowAt indexPath 填充表格的 Cell 的方式。

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

    let cell: SpecificCatagoryBodyCell = tableView.dequeueReusableCell(withIdentifier: "SpecificCatagoryBodyCell") as! SpecificCatagoryBodyCell

    let categoryList = sCategory[indexPath.row]
    cell.product_id.text = String(categoryList.product_id)    
    cell.sCategoryName.text = categoryList.product_title

    let profileImage = categoryList.product_image
    SDWebImageManager.shared().downloadImage(with: profileImage, options: [], progress: nil) { downloadedImage, error, cacheType, isDownloaded, url in
            cell.sCategoryImage.image = downloadedImage
        }

    return cell
}

如果我想填充 tableViewCell ,一切都很好。问题是有时我需要使用不同的数据来拨打任何电话,假设这里我需要来自 JSON 响应的 product_id 并使用相应的号码拨打电话。如何提取 tableView 索引之外的数字,可以将其提取到函数中,将其存储在变量中并使用它。这是我提供的视觉效果,以便更好地理解。在第一张图片中,您可以看到表格中有 Cell

当我点击 Cell 时,它会转到不同的 ViewController ,所以我希望当我点击 Cell 时它会通过 product_id 并使用该 product_id 进行另一个 post 调用以显示产品的详细信息。这是下面的产品详细信息页面。

希望这是有道理的。如果您需要,我很乐意提供更多详细信息。非常感谢。

【问题讨论】:

    标签: swift


    【解决方案1】:

    我可能在这里遗漏了一些细节,但是...

    你的“问题”是你可以有不止一次出现categoryList 项目......这就是我猜你首先使用UITableView 的原因。

    在您的UITableView 中,“我现在应该显示哪个元素”的解决方案由cellForRowAt 提供给您。在该函数中,您只需选择与您现在要填充的单元格匹配的 JSON 项目。

    您可以调用:

    let categoryList = sCategory[indexPath.row]
    

    我假设sCategory 是一组categoryList 项目,然后您在其中要求匹配indexPath.row 的项目。

    因此,如果您想使用sCategory 数组中特定产品的product_id,您需要知道要使用数组中的哪个 产品。

    这可以通过多种方式完成。

    1. 您已经知道要使用哪个索引

      在这种情况下,为您提供product_id 的简单方法可能如下所示:

      func productId(fromIndex index: Int) -> Int? {
          guard sCategory.count > index else { //just a check to make sure the index actually exists in your collection
              return nil
          }
          return sCategory[index].product_id
      }
      

      这样使用:

      if let productId = productId(fromIndex: someIndexYouAlreadyKnow) {
           //You're in business :)
      }
      
    2. 您不知道要使用哪个索引

      在这种情况下,您需要知道要查找的内容,假设您知道 product_title 并且 product_title 是唯一的。

      然后你可以写一个这样的函数:

      func productId(forTitle productTitle: String) -> Int? {
          let candidates = sCategory.filter { $0.product_title == productTitle}
          guard candidates.count == 1 else { //assure that we have exactly one hit
              return nil
          }
          return candidates.first!.product_id
      }
      

      你可以这样使用:

      if let productId = productId(forTitle: "Product 1") {
           //You're in business :)
      }
      

    更新(在看到您更新的问题后)

    你说

    所以我希望当我点击 Cell 时,它会传递相应单元格的 product_id 并使用该 product_id 进行另一个帖子调用以显示产品的详细信息。

    我看你已经问过这个问题了:how do I change an image by selecting a cell in CollectionView

    这里的答案几乎是一样的:)

    UITableView 有一个UITableViewDelegate,就像UICollectionView 有一个UICollectionViewDelegateUITableViewDelegate 可以选择实现的方法之一是tableView(_:didSelectRowAt:),它甚至可以为您提供所选行的indexPath

    因此,如果您有我之前建议的方法productId(fromIndex: ),您可以按照以下方式做一些事情

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let productId = productId(fromIndex: indexPath.row) {
            //you now have the productId matching the selected row, use that for you further adventures :)
        }
    }
    

    希望对你有所帮助。

    【讨论】:

    • 我真的很感谢你的努力,真的很慷慨。请多帮帮我,我很挣扎。我真的很想向你们学习。我已经更新了问题,如果您需要更多详细信息,请告诉我。
    • 是的,它相对简单,我得到了答案。非常感谢您的友好回复。我之前做了一个静态项目,我从那里得到了这个想法,当然你的回答也帮助我得到了这个想法。我是第一次与 json 合作,我的公司为我处理了一个庞大的项目,我作为一名 iOS 开发人员独自在这里工作,没有同事的帮助。因此,有时我会向 StackOverFlow 提问。如果有人指导我解决这个问题,那就相对简单了。
    • 这很容易......当你知道怎么做的时候 :) 很高兴你成功了
    【解决方案2】:

    我终于得到了这样的解决方案

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
        if( segue.identifier == "productList_to_detail" ) {
    
            let VC1 = segue.destination as! ShopItemVC
              if let indexPath = self.tableView.indexPathForSelectedRow {
                    let productIdToGet = sCategory[indexPath.row]
                    VC1.product_id = productIdToGet.product_id
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-12-05
      • 2023-03-29
      • 2018-06-02
      • 2021-08-15
      • 2021-02-22
      • 2021-07-11
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多