iOS如何在swift中填充Tableview Header和Section中的数据?

时间:2018-01-24 10:52:37

标签: ios swift uitableview

在我的项目中,我实现了Tableview标题和节功能,但我不知道如何在节中填充数据..

这是来自服务器的数组。

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";
})

我在这里给出了我正在尝试的代码..

我的标题标题数组是,

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

表委托和数据源方法是:

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!
}

以上代码我要根据StatusId进行验证。如果StatusId是2和3意味着我想在第二个Header下添加其他StatusId意味着我想添加在第一个Header下。 请帮我完成这个..

对于点击选项,代码为。

@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()
}

1 个答案:

答案 0 :(得分:2)

您可以为两个不同的部分创建单独的数组

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

将两个数组声明为全局变量

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)")

表视图数据源方法

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!
}