2017-02-05 8 views
-1

私は2つのテーブルビューを持っています。どちらもカス​​タムです。それらの1つは、ヘッダーセルを持つカスタムテーブルビューです。 2番目は、ヘッダーセルのないカスタムTableViewです。私がしようとしているのは、誰かが最初のtableViewをタップすると、タップされた項目が2番目のTableViewに追加されます。問題は、2番目のアイテムを追加しようとすると、アプリケーションがインデックスの範囲外の致命的なエラーでクラッシュすることです。添付インデックスがカスタムテーブルビューで範囲外になっています

if wordData.count > indexPath.row { 

       cell.wordLabel.text = wordData[indexPath.section].data[indexPath.row] //This one 


        cell.wordLabelS.text = wordDataS[indexPath.section].dataS[indexPath.row] 

       } 

はMainStoryboardのいくつかの写真です:

import UIKit 

struct TableData { 

    var section: String = "" 

    var data = Array<String>() 

    var dataS = Array<String>() 

    init(){} 

} 

var data = Array<TableData>() 
var wordData = Array<TableData>() 

class MyCustomCell: UITableViewCell { 

    @IBOutlet var label: UILabel! 

    @IBOutlet var labelS: UILabel! 

} 

class MyCustomWordCell: UITableViewCell { 

    @IBOutlet var wordLabel: UILabel! 
    @IBOutlet var wordLabelS: UILabel! 

} 

class MyCustomHeader: UITableViewCell { 

    @IBOutlet var header: UILabel! 

} 

class TypeViewController: BaseViewController , UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet var tableView: UITableView! 

    @IBOutlet var wordTableView: UITableView! 

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

     if tableView == self.tableView { 
      return data[section].data.count 
     } 

     if tableView == self.wordTableView { 
      return wordData.count 
     } 

     return 0 
    } 


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

     if tableView == self.tableView { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "TypeCell", for: indexPath) as! MyCustomCell 
      cell.label.text = data[indexPath.section].data[indexPath.row] 
      cell.labelS.text = data[indexPath.section].dataS[indexPath.row] 
      return cell 

     } 

     if tableView == self.wordTableView { 

      let cell = tableView.dequeueReusableCell(withIdentifier: "WordCell", for: indexPath) as! MyCustomWordCell 

      if wordData.count != 0 { 
       if wordData.count > indexPath.row { 
        cell.wordLabel.text = wordData[indexPath.row].data[0] 
        cell.wordLabelS.text = wordData[indexPath.row].dataS[0] 
       } 
      } 
      return cell 
     } 
     return UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell") 

    } 

    func numberOfSections(in tableView: UITableView) -> Int { 

     if tableView == self.tableView { 

      return 7 

     } else { 

      return 1 

     } 

    } 

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

     if tableView == self.tableView { 

      let headerCell = tableView.dequeueReusableCell(withIdentifier: "Header") as! MyCustomHeader 

      headerCell.header.text = data[section].section 

      return headerCell 

     } 

     return UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Header") 

    } 

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return 50.0 
    } 

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

     if tableView == self.tableView { 

      var element: TableData 

      element = TableData() 

      element.data.append(data[indexPath.section].data[indexPath.row]); 

      element.dataS.append(data[indexPath.section].dataS[indexPath.row]); 

      wordData.append(element) 

      wordTableView.reloadData() 

      if wordData.count == 3 { 

       performSegue(withIdentifier: "segueFind", sender: self) 

      } 

     } 

     if tableView == self.wordTableView { 

      wordData.remove(at: indexPath.row) 

      wordTableView.reloadData() 

     } 

    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     addSlideMenuButton() 
     addItems() 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    func addItems() { 

     var new_elements:TableData 

     new_elements = TableData() 
     new_elements.section = "Name" 
     new_elements.data.append(obj1); 
     new_elements.data.append(obj2); 
     new_elements.data.append(obj3); 
     new_elements.data.append(obj4); 
     new_elements.data.append(obj5); 
     new_elements.data.append(obj6); 
     new_elements.data.append(obj7); 
     new_elements.dataS.append(objS1); 
     new_elements.dataS.append(objS2); 
     new_elements.dataS.append(objS3); 
     new_elements.dataS.append(objS4); 
     new_elements.dataS.append(objS5); 
     new_elements.dataS.append(objS6); 
     new_elements.dataS.append(objS7); 

     data.append(new_elements) 

     new_elements = TableData() 
     new_elements.section = "Name" 
     new_elements.data.append(obj11); 
     new_elements.data.append(obj12); 
     new_elements.data.append(obj13); 
     new_elements.data.append(obj14); 
     new_elements.data.append(obj15); 
     new_elements.data.append(obj16); 
     new_elements.data.append(obj17); 
     new_elements.dataS.append(objS11); 
     new_elements.dataS.append(objS12); 
     new_elements.dataS.append(objS13); 
     new_elements.dataS.append(objS14); 
     new_elements.dataS.append(objS15); 
     new_elements.dataS.append(objS16); 
     new_elements.dataS.append(objS17); 

     data.append(new_elements) 

     new_elements = TableData() 
     new_elements.section = "Name" 
     new_elements.data.append(obj21); 
     new_elements.data.append(obj22); 
     new_elements.data.append(obj23); 
     new_elements.data.append(obj24); 
     new_elements.data.append(obj25); 
     new_elements.data.append(obj26); 
     new_elements.data.append(obj27); 
     new_elements.dataS.append(objS21); 
     new_elements.dataS.append(objS22); 
     new_elements.dataS.append(objS23); 
     new_elements.dataS.append(objS24); 
     new_elements.dataS.append(objS25); 
     new_elements.dataS.append(objS26); 
     new_elements.dataS.append(objS27); 

     data.append(new_elements) 

     new_elements = TableData() 
     new_elements.section = "Name" 
     new_elements.data.append(obj31); 
     new_elements.data.append(obj32); 
     new_elements.data.append(obj33); 
     new_elements.data.append(obj34); 
     new_elements.data.append(obj35); 
     new_elements.data.append(obj36); 
     new_elements.data.append(obj37); 
     new_elements.dataS.append(objS31); 
     new_elements.dataS.append(objS32); 
     new_elements.dataS.append(objS33); 
     new_elements.dataS.append(objS34); 
     new_elements.dataS.append(objS35); 
     new_elements.dataS.append(objS36); 
     new_elements.dataS.append(objS37); 

     data.append(new_elements) 

     new_elements = TableData() 
     new_elements.section = "Name" 
     new_elements.data.append(obj41); 
     new_elements.data.append(obj42); 
     new_elements.data.append(obj43); 
     new_elements.data.append(obj44); 
     new_elements.data.append(obj45); 
     new_elements.data.append(obj46); 
     new_elements.data.append(obj47); 
     new_elements.dataS.append(objS41); 
     new_elements.dataS.append(objS42); 
     new_elements.dataS.append(objS43); 
     new_elements.dataS.append(objS44); 
     new_elements.dataS.append(objS45); 
     new_elements.dataS.append(objS46); 
     new_elements.dataS.append(objS47); 

     data.append(new_elements) 

     new_elements = TableData() 
     new_elements.section = "Name" 
     new_elements.data.append(obj51); 
     new_elements.data.append(obj52); 
     new_elements.data.append(obj53); 
     new_elements.data.append(obj54); 
     new_elements.data.append(obj55); 
     new_elements.data.append(obj56); 
     new_elements.data.append(obj57); 
     new_elements.dataS.append(objS51); 
     new_elements.dataS.append(objS52); 
     new_elements.dataS.append(objS53); 
     new_elements.dataS.append(objS54); 
     new_elements.dataS.append(objS55); 
     new_elements.dataS.append(objS56); 
     new_elements.dataS.append(objS57); 

     data.append(new_elements) 

     new_elements = TableData() 
     new_elements.section = "Name" 
     new_elements.data.append(obj61); 
     new_elements.data.append(obj62); 
     new_elements.data.append(obj63); 
     new_elements.data.append(obj64); 
     new_elements.data.append(obj65); 
     new_elements.data.append(obj66); 
     new_elements.data.append(obj67); 
     new_elements.dataS.append(objS61); 
     new_elements.dataS.append(objS62); 
     new_elements.dataS.append(objS63); 
     new_elements.dataS.append(objS64); 
     new_elements.dataS.append(objS65); 
     new_elements.dataS.append(objS66); 
     new_elements.dataS.append(objS67); 

     data.append(new_elements) 


    } 

} 

エラーが指定された行は次のようである:これはスウィフトである。ここ

3.私のコードです

https://www.dropbox.com/sh/s5k50ubas8wewo8/AACTkAsPDtgbOk3EOrb0LeDJa?dl=0

+0

どのラインが範囲外の例外を与えますか? – Paulw11

+0

申し訳ありませんが、私はそれを追加するのを忘れました。ラインが追加されました。 – kps2501

+0

これは 'wordData [indexPath.section] .count'でなければなりません – Paulw11

答えて

0

あなたのデータ構造は実際に役に立たないあなたの問題は、あなたのcellForRow:at:の機能でindexPath.sectionwordTableViewに使用していることです。

この関数は、wordData[indexPath.row].data[0]を使用する必要があります。

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

    if tableView == self.tableView { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "TypeCell", for: indexPath) as! MyCustomCell  
     cell.label.text = data[indexPath.section].data[indexPath.row] 
     cell.labelS.text = dataS[indexPath.section].dataS[indexPath.row] 
     return cell 

    } 

    if tableView == self.wordTableView { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "WordCell", for: indexPath) as! MyCustomWordCell 

     if wordData.count != 0 { 
      if wordData.count > indexPath.row { 
       cell.wordLabel.text = wordData[indexPath.row].data[0]    
       cell.wordLabelS.text = wordDataS[indexPath.row].dataS[0]  
      }   
     } 
     return cell   
    } 
    return UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")  
} 

また、あなたのnumberOfRowsInSectionが正しくありません。それはする必要があります:

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

    if tableView == self.tableView { 
     return data[section].data.count 
    } 

    if tableView == self.wordTableView { 
     return wordData.count 
    } 

    return 0 
} 
+0

答えをありがとうが、私はまだWordListに1項目を追加することができます。 didSelectRowAtPathコードで問題になることはありますか? – kps2501

+0

申し訳ありませんが、私もあなたの 'numberOfRowsInSection'を修正しなければならなかったことを忘れていました – Paulw11

+0

' wordTableView'のデータソースが 'var wordTableData = TableData()'だった場合、 'と' wordLabelS'の配列は – Paulw11

関連する問題