私は、関数(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
}
は私たちにいくつかのコードを表示し、我々は助けになるために喜んでいるでしょう。 – saopayne
あなたは何が助けを必要としているかについてより具体的にする必要があります。多くの場合、ネットワーク要求の非同期性に問題があり、非同期呼び出しを行い、完了ブロック内のデータを処理するコードを置くのではなく、呼び出し直後にデータが存在することを期待します。 –
私は@sapopayneを書いたコードを追加しました – abha