2017-06-24 17 views
0

私は3つの配列を持っています。 1つは、各セクションのヘッダーをtableViewに表示し、1つはタイトルを表示し、1つは各セルのリンクを表示します。私はこのコードを実行すると、私が手:(Swift)致命的なエラー:インデックスが範囲外です

fatal error: Index out of range 

Here is what happens after it runs

私のコードは次のとおりです。デバッガで

import UIKit 

class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
    var headers = ["Bein Sports", "Sky Sports", "Alkass", "Other"] 

    var channels = [["Bein Sports 1","Bein Sports 2","Bein Sports 3","Bein Sports 4","Bein Sports 5","Bein Sports 6","Bein Sports 7","Bein Sports 8","Bein Sports 9","Bein Sports 10","Bein Sports News"], 
           ["Sky Sports 1","Sky Sports 2","Sky Sports 3","Sky Sports 4","Sky Sports 5"], 
           ["Alkass One", "Alkass Two", "Alkass Three", "Alkass Four", "Alkass Five"], 
           ["BT Sports 1", "BT Sports 2", "Real Madrid TV", "Real Madrid TV 2"]] 

    var links = [["https://www.google.ca","https://www.facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com"], 
       ["https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com"], 
       ["https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com"], 
       ["http://twitter.com","http://facebook.com","http://www.google.com","http://www.instagram.com"]] 

    var myIndex = 0 

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

    func numberOfSections(in tableView: UITableView) -> Int { 
     return channels.count //Rows 
    } 

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return headers[section] //Sections 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
     cell.textLabel?.text = channels[indexPath.section][indexPath.row] //TextLabel 
     return cell 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     myIndex = [indexPath.section][indexPath.row] 
     performSegue(withIdentifier: "segue", sender: self) 
    } 
} 

これは強調表示された行である:

myIndex = [indexPath.section][indexPath.row] 
+1

'IndexPath'(section、row)の値は何ですか? –

+0

これは[あなたの最後の質問で]同じ問題です(https://stackoverflow.com/questions/44712​​469/swift-array-of-urls-not-working)。あなたはそこでの答えとあなたが行った変更を見直しましたか? – rmaddy

+0

@rmaddyこれは正しい質問です。 –

答えて

0

することができますmyIndexをIndexPathとして保存し、myIndexを使用してindexPathの行とセクションを取得します。

 var myIndex : IndexPath = IndexPath() 
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     myIndex = NSIndexPath(row: indexPath.section, section: indexPath.row) as IndexPath 
     performSegue(withIdentifier: "segue", sender: self) 
} 
+0

Swiftで 'NSIndexPath'を使用しないでください。 – rmaddy

+0

@rmaddyなぜですか? –

+0

@ KarrarAl-Mimarあなたは 'IndexPath'を使用しています。 'NSIndexPath'を作成して' IndexPath'にキャストするのはなぜですか?それは無意味です。それは迅速です。 Objective-C APIではなくSwift APIを使用します。 – rmaddy

関連する問題