gpt4 book ai didi

iOS 如何快速填充 Tableview 标题和部分中的数据?

转载 作者:行者123 更新时间:2023-11-28 12:11:20 25 4
gpt4 key购买 nike

在我的项目中,我实现了 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"]]

表委托(delegate)和数据源方法是:

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

最佳答案

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

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

TableView 数据源方法

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

关于iOS 如何快速填充 Tableview 标题和部分中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48420667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com