2017-05-31 8 views
0

PLISTを使用してプロパティを設定するtableViewを実装しました。特定のセルにセクションを追加する

特定の行に3つのセクションを追加したいと思います。 (行12、行24、行35)

私は次のコードで試しましたが、コードが多すぎてうまく機能しません。

以下に画像とコードが追加されています。

enter image description here

import UIKit 
class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

     @IBOutlet var tblStoryList: UITableView!  
      var array = PLIST.shared.mainArray 

     var array = PLIST.shared.mainArray 
     let sections: [String] = ["First stage","Second Stage","Third Stage"] 
     let s1Data : [String] = ["Row1","Row2","Row3"] 
     let s2Data : [String] = ["Row4","Row5","Row6"] 
     let s3Data : [String] = ["Row7","Row8","Row9"] 

     var sectionData: [Int: [String]] = [:] 

     override func viewDidLoad() { 
      super.viewDidLoad() 

      sectionData = [0: s1Data, 1: s2Data, 2: s3Data] 
    } 


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

     func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
      return sections[section] 
     } 
     func numberOfSections(in tableView: UITableView) -> Int { 
      return 3 
     } 

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

    UITableViewCell { 


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


    //making plist file 
    let dict = self.array[indexPath.row] 
    let title = dict["title"] as! String 
    let imageName = dict["image"] as! String 
    let temp = dict["phrases"] as! [String:Any] 
    let arr = temp["array"] as! [[String:Any]] 
    let detail = "progress \(arr.count)/\(arr.count)" 

    //property to plist file 
    cell.imgIcon.image = UIImage.init(named: imageName) 
    cell.lblTitle.text = title 
    cell.lblSubtitle.text = detail 

    cell.selectionStyle = UITableViewCellSelectionStyle.none 


    return cell 
} 
+0

現在、 'sectionData'ディクショナリに静的データを設定していて、plist配列で' cellForRowAt'のデータを設定しているので、tableViewに表示する実際の応答を表示する必要があります。また、第2セクションが第12行に来ることをどのように知っていますか? –

答えて

1

indexPath.rowあなたがのtableViewのcellForRowAtになっているが、セクションに相対的です。メイン配列(すべての行を持つ)のインデックスとして直接使用することはできません。

あなたは(前のセクションの総アイテム数と行を相殺することによって)その配列のインデックスにindexPath.rowを変換する簡単な計算を実行する必要があります。

let index = [0,12,36][indexPath.section] + indexPath.row 
let dict = array[index] 

同じことが当てはまりますあなたはnumberOfRowsInSectionに与える応答に:

return [12,24,35][section] 

私は、データ構造(PLIST)が、それは常に正確エントリのこれらの数が含まれており、決して変わらないように剛性であろうと、それは少し奇妙見つけます。ハードコーディングされた番号(例:12,24,35,36)をその場所全体に広げないようにするだけであれば、より一般化されたアプローチを提案します。例えば

:(あなたが他の場所で他の目的のためにそれを使用している場合を除き)

// declare section attributes in your class 
    let sectionTitles = ["First stage","Second Stage","Third Stage"] 
    let sectionSizes = [12,24,35] // central definition, easier to maintain (or adjust to the data) 
    let sectionOffsets = sectionSizes.reduce([0]){$0 + [$0.last!+$1] } 

    // and use them to respond to the table view delegate ... 

    let index = sectionOffsets[indexPath.section] + indexPath.row 
    let dict = array[index] 
    // ... 

    return sectionSizes[section] // numberOfRowsInSection 

このアプローチを使用して、sectionDataを作成する必要はありません。

サンプルコードでは、sectionDataの内容は予測されるセクションサイズと一致しないデータでハードコードされているため、正しいインデックス計算でも機能しません。

+0

ありがとう、私はそれをやろうとします:) – risa8

0

あなたはのtableViewでスイッチケースを使用しようとすることができます:cellForRowAtIndexPath:

関連する問題