2017-05-31 96 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:

相关问题