Home Php C# Sql C C++ Javascript Python Java Go Android Git Linux Asp.net Django .net Node.js Ios Xcode Cocoa Iphone Mysql Tomcat Mongodb Bash Objective-c Scala Visual-studio Apache Elasticsearch Jar Eclipse Jquery Ruby-on-rails Ruby Rubygems Android-studio Spring Lua Sqlite Emacs Ubuntu Perl Docker Swift Amazon-web-services Svn Html Ajax Xml Java-ee Maven Intellij-idea Rvm Macos Unix Css Ipad Postgresql Css3 Json Windows-server Vue.js Typescript Oracle Hibernate Internet-explorer Github Tensorflow Laravel Symfony Redis Html5 Google-app-engine Nginx Firefox Sqlalchemy Lucene Erlang Flask Vim Solr Webview Facebook Zend-framework Virtualenv Nosql Ide Twitter Safari Flutter Bundle Phonegap Centos Sphinx Actionscript Tornado Register | Login | Edit Tags | New Questions | 繁体 | 简体


3 questions online user: 8

0
votes
answers
39 views
+10

未在單元格內顯示JSON響應

-1

我正在創建Tableview並嘗試在單元格中包含一個通過API從JSON接收的信息。 信息(JSON)收到的很好,並在變量內正確記錄。 但是,我可以發現的是,由於收到的信息帶有一個小的「延遲」,因此不會在單元格創建時刻將單元格的標籤文本設置爲默認變量內容。 我想解決的辦法是在解析JSON內容的時刻更新標籤,對吧?我該怎麼做呢? (更新單元格的標籤後,它已被創建) 任何其他洞察力/解決方案非常感謝。未在單元格內顯示JSON響應

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, AddStock { 

    let operacoesAcoes = ListaOperacoes() 
    var todasAsOperacoes : [Operacao] = [] 

    @IBOutlet weak var acoesTableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     acoesTableView.delegate = self 
     acoesTableView.dataSource = self 
     acoesTableView.register(UINib(nibName: "StandardStockListCell", bundle: nil), forCellReuseIdentifier: "standardCell") 
     operacoesAcoes.consolidaAcoes() 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return operacoesAcoes.carteiraAcoes.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "standardCell", for: indexPath) as! StandardStockListCell 
     let informacaoCompletaAcao = operacoesAcoes.carteiraAcoes[indexPath.row] 

     cell.codigoTextField.text = informacaoCompletaAcao.codigoAcao 
     cell.nomeTextField.text = informacaoCompletaAcao.nomeAcao 
     cell.quantidadeTotal.text = String(informacaoCompletaAcao.quantidadeTotal) 
     cell.precoMedioLabel.text = String(format: "%.2f", informacaoCompletaAcao.precoMedio) 

     // 
     // This is the part of the code that should set one label with a value returned from "buscaCotacao" but it does not work 
     // because at the time the cell is displayed it is still not updated from JSON information: 
     // Note: the buscaCotacao func is working fine 

     cell.precoAtualLabel.text = buscaCotacao(ativo: informacaoCompletaAcao.codigoAcao) 

     return cell 
    } 
沙发
0
1

您需要在接收並解析JSON之後重新加載主線程上的表視圖。

self.acoesTableView.reloadData()

板凳
0
0

我做了一些研究和選拔賽,並在接收到請求的結果後能想出一個非常簡單的(和現在很明顯)解決方案來更新我的標籤: - 我把它檢索功能來自API的更新信息的信息(「buscaCotacao」),包括[cell row]信息 - 我從函數內部更新單元格的標籤,僅在收到回覆後纔會發生:

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

let cell = tableView.dequeueReusableCell(withIdentifier: "standardCell", for: indexPath) as! StandardStockListCell 
    let informacaoCompletaAcao = operacoesAcoes.carteiraAcoes[indexPath.row] 

    cell.codigoTextField.text = informacaoCompletaAcao.codigoAcao 
    cell.nomeTextField.text = informacaoCompletaAcao.nomeAcao 
    cell.quantidadeTotal.text = String(informacaoCompletaAcao.quantidadeTotal) 
    cell.precoMedioLabel.text = "R$ "+String(format: "%.2f", informacaoCompletaAcao.precoMedio) 

    buscaCotacao(ativo: informacaoCompletaAcao.codigoAcao, for: cell, indexPath: indexPath) 

    return cell 
} 

而在功能:

FUNC buscaCotacao(ativo:字符串,用於細胞:StandardStockListCell,indexPath:IndexPath){

let finalURL = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&interval=1min&apikey=XXXXXXXXXXXXXX&outputsize=compact&symbol=" + ativo 

    Alamofire.request(finalURL, method: .get) 
     .responseJSON { response in 
      if response.result.isSuccess { 
       let resultadoJSON : JSON = JSON(response.result.value!) 
       let resultado = Float (self.parseResultado(json: resultadoJSON))! 
       cell.precoAtualLabel.text = "R$ "+String(format: "%.2f", resultado) 
       self.cotacoes[ativo] = resultado 

      } else { 
       cell.precoAtualLabel.text = "N/D" 
       print("Error: (response.result.error!)") 
      } 
    } 
} 
0
votes
answers
49 views
+10

Collectionviewcell自動佈局高度,動態高度,內容高度

0

尋找幾個小時試圖找到答案!Collectionviewcell自動佈局高度,動態高度,內容高度

我需要我的collectionviewcells高度來調整大小後的單元格內容的大小。任何人都有這個解決方案?

感謝,勞埃德

沙发
0
0

這是不可能在細胞內被改變來調整UICollectionViewCell的高度內容大小後。您可以設置單元格的自定義高度,並將特定的自動佈局約束設置爲單元格內的對象。

板凳
0
0

您的集合視圖代表應該實現方法collectionView(_:layout:sizeForItemAt:),您可以在其中手動計算大小或使用方法systemLayoutSizeFitting(_:)

0
votes
answers
52 views
+10

Swift:如何從UIButton CollectionView cell調用json id

0

我在UICollectionViewCell中有一個UIButton。想從這個單元格獲得json id。我完美地解析了UICollectionViewController中的json數據。當我點擊UICollectionViewCell中的UIButton時,我總是收到end id的問題。Swift:如何從UIButton CollectionView cell調用json id

這裏是我的代碼

的Json

{ 
    "error": false, 
    "product_sellers": [ 
    { 
     "id": 5, 
     "store_name": "ahmedstore", 
     "photo": "user_12-06-2017-12-32-56.png", 
     "is_favorite": 0 
    }, 
    { 
     "id": 122, 
     "store_name": "aitldev6", 
     "photo": null, 
     "is_favorite": 0 
    }, 
    { 
     "id": 123, 
     "store_name": "aitlmanager", 
     "photo": "user_2017-08-31-10-06-am (16).jpg", 
     "is_favorite": 0 
    }, 
    { 
     "id": 135, 
     "store_name": "ajanta library", 
     "photo": "user_2017-08-19-7-16-am23931.png", 
     "is_favorite": 0 
    }, 

CollectionViewController

類ProductStoreCollectionViewController:UICollectionViewController,UICollectionViewDelegateFlowLayout {

override func viewDidLoad() { 

    super.viewDidLoad() 


    let url = URL(string: "..........") 


    URLSession.shared.dataTask(with:url!) { (urlContent, response, error) in 

     if error != nil { 
      print(error ?? 0) 
     } 
     else { 
      do { 

       let items = json["product_sellers"] as? [[String: Any]] ?? [] 

       self.arrStore = [Store]() 

       for item in items { 


        let oStore = Store() 

        oStore.id = item["id"] as? Int 
        oStore.image = item["photo"] as? String 
        oStore.store_name = item["store_name"] as? String 
        oStore.is_favorite = item["is_favorite"] as? Int 

        ProductStoreId.store_id = oStore.id! 

        self.arrStore.append(oStore) 

       } 

      } catch let error as NSError { 
       print(error) 
      } 

      DispatchQueue.main.async(execute: { 

       self.collectionView?.reloadData() 
      }) 

     } 



    }) 

}

UICollectionViewCell

class StoreCollectionViewCell: BaseCell{ 



    let heartBtn: UIButton = { 

     let btn = UIButton() 
     btn.setImage(UIImage(named: "heart_white_copy"), for: .normal) 

     btn.translatesAutoresizingMaskIntoConstraints = false 
     return btn 
    }() 

    var sellerId2: Int = 1 


    func heartBtnClick(){ 

     print("box Btn click") 


     self.sellerId2 = ProductStoreId.store_id 


     print("add to favoriteStore(self.sellerId2)") 


    } 


    override func setupCell() { 

     heartBtn.addTarget(self, action: #selector(heartBtnClick), for: .touchUpInside) 
     addSubview(heartBtn) 




    } 

} 
+0

請添加代碼正確地分配ID

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { // Your code let store = self.arrStore[indexPath.row] // Please correct it if not cell.sellerId2 = store.id } 

,然後在StoreCollectionViewCell類,也可以添加的CollectionView方法。 – ivarun

沙发
0
2

這是因爲你的ProductStoreId.store_id有,當你分析它存儲的最後一個ID。

您需要在您的數據源方法

func heartBtnClick(){ 
    print("box Btn click") 
    //We don't need the below line 
    //self.sellerId2 = ProductStoreId.store_id 
    print("add to favoriteStore(self.sellerId2)") 
} 
+0

謝謝你的回答。我的問題是解決 – Minzu

+0

你能幫我回答這個問題嗎?我想通過點擊按鈕來檢測tableview單元格中的一個按鈕。這裏是鏈接https://stackoverflow.com/questions/48970198/how-to-detect-one-button-in-tableview-cell?noredirect=1#comment84948065_48970198 – Minzu