简体   繁体   中英

iOS How to populate the Data in Tableview Header and Section in swift?

In My Project I Implement Tableview Header and Section functionality but I don't know how to populate data in sections..

This is my array from server.

MyArray : (
    {
    InspectionId = 155;
    InspectionLogId = 102;
    InspectionType = "Property Owner";
    InspectionTypeId = 1;
    ScheduledDate = "2018-01-23T00:00:00";
    Status = Done;
    StatusId = 2;
    TemplateId = 1113;
    TemplateName = "Home validation";
},{
    InspectionId = 158;
    InspectionLogId = 100;
    InspectionType = "Property Owner";
    InspectionTypeId = 1;
    ScheduledDate = "2018-01-23T00:00:00";
    Status = New;
    StatusId = 1;
    TemplateId = 1113;
    TemplateName = "Home validation";
},{
    InspectionId = 145;
    InspectionLogId = 98;
    InspectionType = "Property Owner";
    InspectionTypeId = 1;
    ScheduledDate = "2018-01-23T00:00:00";
    Status = Completed;
    StatusId = 3;
    TemplateId = 1113;
    TemplateName = "Home validation";
},{
    InspectionId = 198;
    InspectionLogId = 988;
    InspectionType = "Property Owner";
    InspectionTypeId = 1;
    ScheduledDate = "2018-01-23T00:00:00";
    Status = Progress;
    StatusId = 4;
    TemplateId = 1113;
    TemplateName = "Home validation";
},{
    InspectionId = 78;
    InspectionLogId = 987;
    InspectionType = "Property Owner";
    InspectionTypeId = 1;
    ScheduledDate = "2018-01-23T00:00:00";
    Status = Corrections;
    StatusId = 5;
    TemplateId = 1113;
    TemplateName = "Home validation";
})

Here I give my Code What I am trying..

My Header title array was,

HeaderArray = [["inspection_Header":"Current Inspection"], ["inspection_Header":"Past Inspection"]]

The Table Delegate and Datasource Method is:

func numberOfSections(in tableView: UITableView) -> Int {
    for _ in 0..<HearderArray.count {
        hidden.append(true)
    }
    return HearderArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if hidden[section] {
        return 0
    } else {
        return MyArray.count
    }
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView()
    headerView.backgroundColor = Colors().appColor
    headerView.tag = section

    let label = UILabel()
    label.text = (HearderArray[section] as AnyObject).value(forKey: "inspection_Section") as? String
    label.frame = CGRect(x: 10, y: 5, width: 150, height: 35)
    label.font = UIFont.boldSystemFont(ofSize: 15)
    headerView.addSubview(label)
    label.tag = section
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 40
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "PropertyInspectionTableViewCell", for: indexPath) as? PropertyInspectionTableViewCell
    if cell == nil {
        cell = UITableViewCell(style: .default, reuseIdentifier: "PropertyInspectionTableViewCell") as? PropertyInspectionTableViewCell;
    }
   let inspectionStatusId:Int = (MyArray[indexPath.row] as AnyObject).value(forKey: "StatusId") as! Int
        print(inspectionStatusId)
    return cell!
}

The above code I want to validate based on StatusId,. If StatusId was 2 and 3 means I want to add under the second Header else other StatusId means I want to add Under the first Header. Please Help me to done this..

For tap option the code was.

@objc func tapFunction(sender:UITapGestureRecognizer) {
    let section = sender.view!.tag
    let indexPaths = (0..<MyArray.count).map { i in return IndexPath(item: i, section: section)  }
    hidden[section] = !hidden[section]
    scheduleInspectionTableView.beginUpdates()
    if hidden[section] {
        scheduleInspectionTableView.deleteRows(at: indexPaths, with: .fade)
    } else {
        scheduleInspectionTableView.insertRows(at: indexPaths, with: .fade)
    }
    scheduleInspectionTableView.endUpdates()
}

You can create separate array for two different sections

var section1Data:[[String:String]] = []
var section2Data:[[String:String]] = []

Declare both arrays as global variable

let MyArray = [
    [
        "InspectionId" : "155",
        "InspectionLogId" : "102",
        "InspectionType" : "Property Owner",
        "InspectionTypeId" : "1",
        "ScheduledDate" : "2018-01-23T00:00:00",
        "Status" : "Done",
        "StatusId" : "2",
        "TemplateId" : "1113",
        "TemplateName" : "Home validation"
    ],[
        "InspectionId" : "155",
        "InspectionLogId" : "102",
        "InspectionType" : "Property Owner",
        "InspectionTypeId" : "1",
        "ScheduledDate" : "2018-01-23T00:00:00",
        "Status" : "Done",
        "StatusId" : "1",
        "TemplateId" : "1113",
        "TemplateName" : "Home validation"
    ],[
        "InspectionId" : "155",
        "InspectionLogId" : "102",
        "InspectionType" : "Property Owner",
        "InspectionTypeId" : "1",
        "ScheduledDate" : "2018-01-23T00:00:00",
        "Status" : "Done",
        "StatusId" : "3",
        "TemplateId" : "1113",
        "TemplateName" : "Home validation"
    ],[
        "InspectionId" : "155",
        "InspectionLogId" : "102",
        "InspectionType" : "Property Owner",
        "InspectionTypeId" : "1",
        "ScheduledDate" : "2018-01-23T00:00:00",
        "Status" : "Done",
        "StatusId" : "4",
        "TemplateId" : "1113",
        "TemplateName" : "Home validation"
    ],[
        "InspectionId" : "155",
        "InspectionLogId" : "102",
        "InspectionType" : "Property Owner",
        "InspectionTypeId" : "1",
        "ScheduledDate" : "2018-01-23T00:00:00",
        "Status" : "Done",
        "StatusId" : "5",
        "TemplateId" : "1113",
        "TemplateName" : "Home validation"
    ]]

   self.section2Data = MyArray.filter({$0["StatusId"] == "2" || $0["StatusId"] == "3"})
   self.section1Data = MyArray.filter({!($0["StatusId"] == "2" || $0["StatusId"] == "3")})

    print("section2Count: \(section2Data)")
    print("section1Count: \(section1Data)")

Table view datasource methods

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

    if section == 0 {

        return self.section1Data.count
    }else {
        return self.section2Data.count
    }
}


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

    var cell = tableView.dequeueReusableCell(withIdentifier: "PropertyInspectionTableViewCell", for: indexPath) as? PropertyInspectionTableViewCell
    if cell == nil {
        cell = UITableViewCell(style: .default, reuseIdentifier: "PropertyInspectionTableViewCell") as? PropertyInspectionTableViewCell;
    }

    var inspectionStatusId:Int = -1
    if indexPath.section == 0 {

       inspectionStatusId = (self.section1Data[indexPath.row] as AnyObject).value(forKey: "StatusId") as! Int

    }else {
        inspectionStatusId = (self.section2Data[indexPath.row] as AnyObject).value(forKey: "StatusId") as! Int
    }
    print(inspectionStatusId)
    return cell!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM