2017-05-31 99 views
0

我已經使用PLIST實現了一個tableView來設置屬性。在特定單元格中添加節

我想在特定的行添加三個部分。 (第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

您需要向我們展示您希望在tableView中顯示的實際響應,因爲您目前正在'sectionData'字典中設置靜態數據,並使用plist數組在'cellForRowAt'中設置數據。另外你怎麼知道第二部分在第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:

相關問題