如何将动态数据放在UITableview的Section Row swift3中?

时间:2017-08-24 08:04:46

标签: ios uitableview swift3

我有一个UITableview。在这个tableview中,我有5个部分与差异。没有。的行。我把静态数据放在部分的行中(工作正常),但我无法将动态数据放在这些行中,并且当点击它时,最后一部分不会自动滚动。我正在使用Swift 3。

代码: -

import UIKit
import AVFoundation
import SDWebImage

// MARK: - Section Data Structure
struct Section {

   var name: String!
   var description : String!
   var images : [String]!
   var Headerimages : String!
   var detail : [String]!
   var items: [String]!
   var collapsed: Bool!

   init(name: String, description : String, images : [String], Headerimages : String, items: [String], detail : [String], collapsed: Bool = false) {
       self.name = name
       self.description = description
       self.images = images
       self.Headerimages = Headerimages
       self.items = items
       self.detail = detail
       self.collapsed = collapsed
   }
}
class CustomTableViewController: UITableViewController,CollapsibleTableViewHeaderDelegate {

   var sections = [Section]()

   override func viewDidLoad(){
      super.viewDidLoad()


    tableView.delegate = self
    tableView.dataSource = self


    // Initialize the sections array

           sections = [
        Section(

            name: "TestDemo",
            description : "TestDemo TestDemo.",
            images : ["TestDemo", "TestDemo"],
            Headerimages : "TestDemo",
            items: ["TestDemo",
                    "TestDemo"
                    ],

            detail: ["TestDemo.",
                     "TestDemo."
                     ],
            collapsed : true),
         Section(

            name: "TestDemo",
            description : "TestDemo TestDemo.",
            images : ["TestDemo", "TestDemo"],
            Headerimages : "TestDemo",
            items: ["TestDemo",
                    "TestDemo"
                    ],

            detail: ["TestDemo.",
                     "TestDemo."
                     ],
            collapsed : true),

       ]

    override func viewWillLayoutSubviews() {

       tableView.separatorStyle = .none
    }

    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
         CellAnimator.animateCell(cell, withTransform: CellAnimator.TransformTilt, andDuration: 1)
    }

   // MARK: - View Controller DataSource and Delegate

  override func numberOfSections(in tableView: UITableView) -> Int {

//print("Section count--- \(sections.count)")
    return sections.count
   }

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sections[section].items.count
}


   // Cell
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> SubCategoryTableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "soundElementalcell") as! SubCategoryTableViewCell


    let imageNames = sections[indexPath.section].images[indexPath.row]


    cell.Labeltittle.text = sections[indexPath.section].items[indexPath.row]
    cell.Labeltittle.textColor = UIColor(hex : 0x90BA27)

    cell.LabelDetail.textColor = UIColor.white

    cell.LabelDetail.text = sections[indexPath.section].detail[indexPath.row]

    cell.LabelDetail.textColor = UIColor.white
    cell.backgroundColor = UIColor.clear
    cell.IconView.image = UIImage(named: imageNames)


    cell.IconView.layer.cornerRadius = cell.IconView.frame.height / 2
     cell.IconView.clipsToBounds = true
   cell.selectionStyle = .none


     return cell
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return sections[indexPath.section].collapsed! ? 0 : 80
}


// Header
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header")

    let imageNames =  sections[section].Headerimages

    header.titleLabel.text = sections[section].name

    header.detailLabel.text = sections[section].description
    header.detailLabel.numberOfLines = 0
    header.detailLabel.lineBreakMode = .byWordWrapping

    header.setCollapsed(collapsed: sections[section].collapsed)
    header.section = section

     header.delegate = self

    return header
}

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    view.tintColor = UIColor.clear
}



override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 90
}


func toggleSection(header: CollapsibleTableViewHeader, section: Int) {
    let collapsed = !sections[section].collapsed

    // Toggle collapse
    sections[section].collapsed = collapsed
    header.setCollapsed(collapsed: collapsed)

    // Adjust the height of the rows inside the section
    tableView.beginUpdates()


    for i in 0 ..< sections[section].items.count {

        let indexPath = IndexPath(item: i, section: section)
        tableView.reloadRows(at: [indexPath], with: .fade)

    }
    tableView.endUpdates()
}


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    performSegue(withIdentifier: "NextViewController", sender: indexPath)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "NextViewController" {

        let NextVC = segue.destination as! NextViewController
        let indexPath = sender as! IndexPath
        NextVC.items = sections[indexPath.section].items[indexPath.row]
       NextVC.name = sections[indexPath.section].name
        NextVC.imageName = sections[indexPath.section].images[indexPath.row]
        NextVC.sectionName = "ELEMENTS"

        NextVC.Subtittle = sections[indexPath.section].description

        //  print("trackName is  pressed...\(sections[indexPath.section].items[indexPath.row])")

      }
   }
}

先谢谢。

0 个答案:

没有答案