2017-03-25 8 views
0

私はUITableViewに複数のセクションを持っています。私はいくつかの値を設定するために、セルのIndexPathを使用しています。私が尋ねるのは、新しいセクションの開始時に、IndexPathのカウントがゼロに再開するかどうかです。どんな助けでも大歓迎です。ありがとう!スワイプIndexPathは複数のセクションにわたって連続してカウントされますか?

+0

'IndexPath'には' section'と 'row'プロパティがあります。各セクション内で、行番号はゼロから始まります。 –

答えて

1

です。以下の基本的な例を実装すると、各セクションの行インデックスが0からどのように再スタートするのかがわかります。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
    @IBOutlet var myTableView: UITableView! 

    let myDataSource = ["Andrew", "Anthony", "Bill", "Brad", "Charlie", "Craig"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     myTableView.delegate = self 
     myTableView.dataSource = self 
    } 

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

     cell.myCellLabel.text = myDataSource[indexPath.row + indexPath.section*2] + " [Row = \(indexPath.row), Section = \(indexPath.section)]" 
     return cell 
    } 

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 2 
    } 

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

     var title = "" 
     switch section { 
     case 0: 
      title = "A" 
     case 1: 
      title = "B" 
     case 2: 
      title = "C" 
     default: 
      break 
     } 

     return title 
    } 

} 
+0

情報ありがとう! –

関連する問題