2016-07-01 13 views
0

私は、関数(func obtainData)を介してウェブサイトデータを取得し、tableViewにいくつかのデータを表示するアプリケーションを作成しようとしています。関数からテーブルビューへのデータの受け渡し

私はindexpath.rowを使用することができるようにウェブサイトからデータを取得する方法を理解しましたが、私はデータを渡す方法を見つけることができませんでしたそれをtableViewに表示すること。

すべてのアイデア!

ありがとうございました

以下は私が書いたコードです。

import UIKit 

    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 



     var recommendation = "" 
     var recommendationArray = "" 
     var delExtraTextArray = [String]() 
     var urlRecommendationArrayStart = [String]() 
     var urlRecommendationArrayEnd = [String]() 
     var RecommendationStart = [String]() 
     var RecommendationEnd = [String]() 

     // need the var below to make the recommendations as an array to be used in a table view as indexpath.row 

     var cellNumber = [String]() 
     var cellTitle = [String]() 
     var cellDetails = [String]() 




     @IBOutlet var tableView: UITableView! 

     override func viewDidLoad() { 
      super.viewDidLoad() 
      obtainData() 
      tableView.delegate = self 
      tableView.dataSource = self 


     } 




     func obtainData() { 

      var url = NSURL (string: "http://www.choosingwisely.org/societies/american-college-of-obstetricians-and-gynecologists") 

      if url != nil { 

       let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in 

        if error == nil { 

         // to get all of the url content and covert them into string 

         var urlContent = NSString(data: data!, encoding: NSUTF8StringEncoding) as NSString! 

         // to get to a specific contect seperated by a string 

         self.urlRecommendationArrayStart = (urlContent?.componentsSeparatedByString("<ol class=\"society-ol\">"))! 

         if self.urlRecommendationArrayStart.count > 0 { 

          self.urlRecommendationArrayEnd = self.urlRecommendationArrayStart[1].componentsSeparatedByString("</ol>") 

          // print(self.urlRecommendationArrayEnd) 

          // to check if there is any extra not needed text at the end of the recommnedations in the source page 

          self.delExtraTextArray = self.urlRecommendationArrayEnd[0].componentsSeparatedByString("<p><a") 

          if self.delExtraTextArray.count > 0 { 

           self.recommendationArray self.delExtraTextArray[0] as! String 
           self.obtainRecommendationTitle() 

    } else { 

           self.recommendationArray = self.urlRecommendationArrayEnd[0] as! String 
           self.obtainRecommendationTitle() 

           // print("method 2 worked") 

          } 


         } else { 

          self.textView.text = "Sorry, couldn't get the recommendation at this point. Please make sure to download the updated version of the app" 
         } 


        } else { 

         self.textView.text = "Please check connection then try again" 

        } 

       }) 

       task.resume() 



      } else { 

       self.textView.text = "Please check connection then try again" 

       } 




     } 


     // to get the title of each recommendation 

     func obtainRecommendationTitle() -> Array<String> { 

      for var i = 2; i < urlRecommendationArrayEnd[0].componentsSeparatedByString("<p>").count - delExtraTextArray.count ; i = i + 4 { 

       self.RecommendationStart = self.delExtraTextArray[0].componentsSeparatedByString("<p>") 

       self.RecommendationEnd = RecommendationStart[i].componentsSeparatedByString("</p>") 

       self.recommendationArray = self.RecommendationEnd[0] as! String 

       self.cellTitle.append(recommendationArray) 

      } 

      return cellTitle 
     } 




     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

      return cellTitle.count 
     } 



     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 


      let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell") 

      cell.textLabel?.text = cellTitle [indexPath.row] 

      return cell 

     } 
+0

は私たちにいくつかのコードを表示し、我々は助けになるために喜んでいるでしょう。 – saopayne

+0

あなたは何が助けを必要としているかについてより具体的にする必要があります。多くの場合、ネットワーク要求の非同期性に問題があり、非同期呼び出しを行い、完了ブロック内のデータを処理するコードを置くのではなく、呼び出し直後にデータが存在することを期待します。 –

+0

私は@sapopayneを書いたコードを追加しました – abha

答えて

1

私が必要と考えるのは、必要なデータでテーブルビューセルの内容を構成することです。この仮定に基づいて、あなたはこのようなものを使用することができます:あなたはいくつかのコードを表示したり、少し良くあなたの問題を説明する場合

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if let cell = tableView.dequeueReusableCellWithIdentifier("PUT_YOUR_CELL_IDENTIFIER_HERE") as? UITableViewCell { 
     let stuff = yourArray[indexPath.row] 
     stuff.some_property_you_want = the_value_you_want  
     return cell 
    } else { 
     return UITableViewCell() 
    } 
} 

を、あなたはここでスタックオーバーフロー上の人々からより良いサポートを取得します。

EDIT(あなたの編集に基づいて): 通常のセルを使用していますか? セルには、必要な文字列を入れるテキストフィールドがありますか? ストーリーボードにセルの識別子「セル」を定義しましたか? tableViewコンセントをtableView自体に接続しましたか?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if let cell = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell { 
     print("ENTERED HERE!") 
     let myCellText = cellTitle[indexPath.row] 
     //what is the result of this print? 
     print("TEXT TO ADD: \(myCellText)") 
     //(...) 
     cell.textLabel?.text = myCellText  
     return cell 
    } else { 
     return UITableViewCell() 
    } 
} 

これらのプリント結果はどうなりますか?

+0

tableView.reloadData()を実行して、tableViewに入れるデータに情報がロードされた後に実行することを確認してください – Sethmr

+0

コードを追加しました@armadeu – abha

+0

コメントにコードを入れることができませんでした。これを確認して、コードに記載されているプリントの出力が何であるか教えてください。 –

2

cellForRowAtIndexPathデリゲートメソッドで渡します。この質問はあまりにもオープンなので固くお答えしますが、オンラインのUITableViewチュートリアルに従えば、このトリックを行うべきです。

この1で一目は、基本的にヒットするように見える:https://www.weheartswift.com/how-to-make-a-simple-table-view-with-ios-8-and-swift/

+0

FYI ... 'cellForRowAtIndexPath'は' delegate'メソッドではなく、 'dataSource'メソッドです。 – Santosh

+0

これはdataSourceプロパティであるUITableViewDataSourceプロトコルのメソッドです。メソッドであるため、プロトコル型のプロパティはデリゲートと呼ばれるため、テーブルビューはcellForRowAtIndexPathの責任をdataSourceとして割り当てられている人に委任します。 – ghostatron

+0

conarchの防御では、テーブルビューなどのオブジェクトは特別なケースデータソースは、基本的には、より具体的なもののための委譲デザインパターンの第2の特殊な使用です。それをdelega (ただし、デリゲートとデータソースの区別を混乱させる危険があるため、そうしないでください)。 –

関連する問題