2017-08-31 5 views
0

4つのセクションと複数の行を持つUiTableViewControllerを作成しました。また、URLの配列を実装しました。私はコーディングに新しいです、これは2つの以前のTableViewControllerの作業の組み合わせですが、私が抱えている問題は、URL配列が各セクションに適用されることです。つまり、セクション1の行1をクリックすると、最初のリンクが開きますが、セクション2の行1をクリックすると最初のリンクも開きます。URLリンクは複数のセクションを持つUiTableViewです

URLアレイを1セクションに制限するにはどうすればよいですか?

私はそれが動作していない理由を理解し、多くのことを試しましたが、これまでのところ得られませんでした。

struct Objects { 
    var sectionName : String! 
    var sectionObjects :[String]! 
} 

var objectsArray = [Objects]() 

let urlArray1 = ["http://www.apple.co.uk","http://www.google.co.uk","https://www.dropbox.com/","tel://123456789",""] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    objectsArray = [Objects(sectionName: "Section 1", sectionObjects: ["one", "two", "three", "four","four A"]), 
        Objects(sectionName: "Section 2", sectionObjects: ["five", "six", "seven", "eight"]), 
        Objects(sectionName: "Section 3", sectionObjects: ["nine", "ten", "eleven", "twelve"]), 
        Objects(sectionName: "Section 4", sectionObjects: ["thirteen", "fourteen", "fifteen", "sixteen"])] 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as UITableViewCell! 

    cell?.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row] 

    return cell! 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return objectsArray[section].sectionObjects.count 
} 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return objectsArray.count 
} 

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return objectsArray[section].sectionName 
} 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let urlString = self.urlArray1[indexPath.row] 
    if let url = URL(string:urlString) 
    { 
     UIApplication.shared.open(url, options: [:]) 
    } 

答えて

0

urlArray1を別々のセクションに設定する必要があります。

let urlString = self.urlArray1[indexPath.row]に電話するときは、セクションではなく行にのみ依存します。したがって(セクション0、行0)、(セクション1、行0)、(セクション2、行0)などはすべて同じ値を返します。

私はあなたのObject構造体にURLプロパティを追加します。

struct Objects { 
    var sectionName: String 
    var sectionObjects: [String] 
    var urlStrings: [String] 
} 

そして、あなたがそうのような適切なものにアクセスできます。

let urlString = objectsArray[indexPath.section].urlStrings[indexPath.row] 

は(あなたのobjectsArrayurlStringsを定義してください)

関連する問題