2016-04-02 22 views
1

私のTableViewControllerのStoryboardで2つのセクションが宣言されています。対応する迅速なクラスでは、私は2セクションと6行を持っているので、テーブルセクションがスウィフトのテーブルビューコントローラーで正しく表示されない

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 2 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return 6 
} 

があります。しかし、これは、セクションが宣言されているので、迷惑である範囲外の連続したインデックスを私に与えています!私は間違って何かをしていますか?

ありがとうございます。これは私のテーブルビューコントローラであるので

UPDATE

わかりました:私は...私のcellForRowに

enter image description here

を書いて何がある:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Identifier", forIndexPath: indexPath) 

    // Configure the cell... 
    cell.textLabel?.text = "Section \(indexPath.section) Row \(indexPath.row)" 

    return cell 


} 

I私は完全にこれが正しいかどうかわからないが、基本的に私が得るもの(cellForRow commenテッドアウトは最初のセクションのみです)

これは役に立ちますか?

+0

'cellForRowAtIndexPath'のコードを追加できますか? – kye

+0

cellForRowAtIndexPathメソッド内には何がありますか? – bolnad

+0

'numberOfRowsInSection'から定数を返すことはまれです。通常、ソースデータ配列からのカウントを返します。 – Paulw11

答えて

0

あなたがそれらを分離する必要があるセクションごとの行数が異なる場合は、合計数を返すことができません:あなたは、静的なコンテンツを持っている場合は、インスペクタウィンドウで

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    switch section { 
    case 0: return 5 
    case 1: return 1 
    default: return 0 
    } 
} 

はそれを設定しよう動的にするには、1つのセルプロトタイプだけが必要です。

enter image description here

+0

ケース1はセクション1ですか? – neX

+0

はい、0が最初のもの、2番目のものなど1 ...インデックス開始が0で –

+0

まだ最初の1つしか残念ながら表示されていません – neX

0

回答

誰が答えに興味があるだけの場合にこんにちはすべて、:スウィフトで

、あなたは静的なセルを使用している場合は、あなたが持っているものではありませんしていますデータソースを使用している場合は、のみnumberOfSectionsInTableViewまたはnumberOfRowsInSectionが必要です。スタティックセルを使用する場合は、必ずこれらの2つの方法を削除してください。

出典:https://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2

は、この情報がお役に立てば幸い!ここで

0

は、私は複数のセクションで私のテーブルビュースミス複数行の設定方法は次のとおりです。この構造体を作成し、その下に、作成、のViewControllerクラスの先頭に

1)を変数

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

var objectsArray = [Objects]() 

2)のviewDidLoadでは、numberOfSectionsInTableView

について)可変

objectsArray = [Objects(sectionName: "Find People", sectionObjects: ["Facebook", "Twitter", "Contacts"]), Objects(sectionName: "Your Account", sectionObjects: ["Edit Info", "Delete Account"]), Objects(sectionName: "Support", sectionObjects: ["Help", "Report a Problem", "Contact Us"]), Objects(sectionName: "App Info", sectionObjects: ["About", "Open Source Libraries", "Privacy Policy", "Our Twitter", "Our Facebook"])] 

セット3発生する任意のアクションのためにtitleForHeaderInSection

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

    return objectsArray[section].sectionName 

} 

7)を設定するためのcellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell 

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

    return cell 
} 

6)についてnumberOfRowsInSection

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

    return objectsArray[section].sectionObjects.count 

} 

5)については

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 

    return objectsArray.count 

} 

4)ユーザーがセルをタップすると、didSelectRowAtIndexPaを呼び出す必要がありますth - >私の場合、4つのセクション(ヘッダー)があるので、4つのスイッチを作成する必要があります(ケースは0ではなく1から始まります)。次に、各セクションについて、各セクションの各行に対して大文字小文字を設定する必要があります。その場合、必要なものを呼び出すことができます。たとえば、別のviewControllerに対してsegueを実行することができます。デフォルトでは、基本的にif else文のelseを参照しています。他のすべてが失敗した場合、何かが正しく動作しなかったというエラーをユーザに表示します。ビルトインされたalertViewを使うことができますが、私はGitHubでSCLAlertViewを使うのが好きです!あなたのケースでは

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    if reachabilityStatus == kNOTREACHABLE { 

     self.displayError("No Internet Connection", message: "Please connect to the internet before continuing") 


    } else { 

     switch(indexPath.section) { 

     case 0: 


      switch(indexPath.row) { 

      case 0: 

       // Find People through facebook -> test below 

       print("Connect with FB") 

       return 


      case 1: 

       // Find People through twitter -> test below 

       print("Connect with Twitter") 

       return 

      case 2: 

       // Find People through contacts -> test below 

       print("Connect with Contacts") 


       return 

      default: 

       self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.") 

       return 

      } 

     case 1: 

      switch(indexPath.row) { 


      case 0: 

       // Edit Account -> test below 

       self.performSegueWithIdentifier("editProfileSegue", sender: self) 

       return 


      case 1: 

       // Delete Account -> test below 

       print("Delete Account") 

       return 


      default: 

       self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.") 

       return 



      } 


     case 2: 


      switch(indexPath.row) { 

      case 0: 

       // Help -> test below 

       print("Help") 

       return 


      case 1: 

       // Report a Problem -> test below 

       print("Report Problem") 

       return 

      case 2: 

       // Contact Us -> test below 

       print("Contact Us") 


       return 

      default: 

       self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.") 

       return 

      } 


     case 3: 


      switch(indexPath.row) { 

      case 0: 

       // About App -> test below 

       print("About App") 

       return 


      case 1: 

       // Open Source Libraries -> test below 

       print("Open Source Libraries") 

       return 

      case 2: 

       // Privacy Policy -> test below 

       print("Privacy Policy") 


       return 

      case 3: 

       // Our Twitter -> Test Below 

       print("Our Twitter") 

      case 4: 

       // Our Facebook -> Test Below 

       print("Our Facebook") 


       return 

      default: 

       self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.") 

       return 

      } 

     default: 

      self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.") 

      print("last error") 

      return 


     } // close section switch 


    } 

} 

、あなただけの3つのセクションを持っているので、ちょうど

が何か助けが必要な場合は、私に教えてくださいどのような作品を参照するためのコードで遊んで!