我已经在UITableView
中创建了包含节和行的DataList。另外,我已经为add & delete
函数实现了委托函数。实际上,我需要添加或追加选择的新section
和row
值。我的第一个模型是PickListData
,然后从Item
-> pickList
中检索其他部分的值。
模型层次结构:PickListData
-> let items: [Item]?
-> var pickList: [SectionList]?
-> textField
我实现了func didAddnewRow(_ sender: UIButton)
,但是在该部分中添加了行,因此我需要在顶部添加新的部分,名称为Selected
。
Section
Selected和从以下部分中选择的行。 附上我想实现的当前和实际图像。
型号:
struct SectionList : Codable {
let title : String?
var items : [Item]?
}
struct PickListData: Codable {
let items: [Item]?
}
struct Item : Codable {
let actionType : Int?
var textField : String?
var pickList: [SectionList]?
var selection: [Item]?
let selectedValue: [String]?
let version: Int?
let masterId: Int?
let itemValue: String?
}
代码:
let decoder = JSONDecoder()
let response = try decoder.decode(PickListData.self, from: data)
let res = response.items?.filter { $0.actionType == 101}
AppData = res?.first
TableView代码部分:
func numberOfSections(in tableView: UITableView) -> Int {
return AppData?.pickList?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DictionaryTableViewCell", for: indexPath) as! DictionaryTableViewCell
let dic = AppData?.pickList?[indexPath.section].items?[indexPath.row]
cell.titleView?.text = dic?.textField
cell.delegate = self
return cell
}
在ViewController中进行代理:
extension PickListViewController: DictionaryTableDelegate {
func didAddnewRow(_ sender: UIButton) {
if let cell = sender.superview?.superview as? DictionaryTableViewCell,
let indexPath = self.tableView.indexPath(for: cell)
{
if let selectedItem = AppData?.pickList?[indexPath.section].items?[indexPath.row] {
let insertIndexPath = IndexPath(item: AppData?.pickList?[0].items?.count ?? 0, section: 0)
AppData?.pickList?[0].items?.append(selectedItem)
cell.btnAdd.isHidden = true
cell.btnDelete.isHidden = false
tableView.beginUpdates()
tableView.insertRows(at: [insertIndexPath], with: .automatic)
tableView.endUpdates()
}
}
}
func didDeleteRow(_ sender: UIButton) {
print("delete")
}
}
当前JSON:
{
"items": [
{
"actionType": 101,
"version": 4,
"pickList": [
{
"title": "S",
"items": [
{
"textField": "Sayaç Yeri Seçiniz",
"itemValue": "0"
},
{
"textField": "Sayaç daire girişinde",
"itemValue": "1"
},
{
"textField": "Sayaç apt. girişinde",
"itemValue": "2"
},
{
"textField": "Sayaç bodrumda",
"itemValue": "3"
},
{
"textField": "Sayaç çatı katında",
"itemValue": "4"
},
{
"textField": "Sayaç bahçede (Müstakil)",
"itemValue": "5"
},
{
"textField": "Sayaç bina dışında",
"itemValue": "6"
},
{
"textField": "Sayaç balkonda",
"itemValue": "7"
},
{
"textField": "Sayaç daire içinde",
"itemValue": "8"
},
{
"textField": "Sayaç istasyon içinde",
"itemValue": "9"
}
]
}
]
}
]
}
新添加的部分是这样的>>
"pickList": [
{
"title": "Selected",
"items": [
{
"textField": "Marka Seçilmedi"
},
{
"textField": "Cihaz Yok"
},
{
"textField": "Yedek Cihaz"
}
]
}
附加节和行:
我已经在UITableView中创建了具有节和行的DataList。另外,我已经实现了添加和删除功能的委托功能。实际上,我需要添加或附加新的节和行值...
您在向该部分添加行之前忘记添加新的部分。插入“选定”部分后,“ A。项目”部分将向下移动。