ios - 在 tableView 数据值 Swift 顶部添加或附加行的部分
问题描述
我已经创建了一个UITableView
包含部分和行的 DataList。另外,我已经为函数实现了委托add & delete
函数。实际上我需要添加或附加新的section
和row
选择的值。我的第一个模型是PickListData
从Item
->检索的其他部分值pickList
。
模型层次结构:PickListData
-> let items: [Item]?
-> var pickList: [SectionList]?
->textField
我实现func didAddnewRow(_ sender: UIButton)
了,但它在部分中添加了行,我需要在顶部添加新部分,名称为Selected
.
- 问题是如何放置
Section
Selected和从以下部分中选择的行。
附上我想要实现的当前和实际的图像。
解决方案
在向该部分添加行之前,您忘记添加新部分。插入“Selected”部分后,“A. Items”部分将下移。
func numberOfSections(in tableView: UITableView) -> Int {
return AppData?.pickList?.count ?? 0
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return [“Selected", “A. Items"]
}
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)
if !AppData?.pickList.contains(where: {$0.title == “Selected" }) {
AppData?.pickList.append(SectionList(title: “Selected”, items: selectedItem))
tableView.insertSections(IndexSet([selectedItem]), with: .automatic)
} else {
AppData?.pickList?[0].items?.append(selectedItem)
}
cell.btnAdd.isHidden = true
cell.btnDelete.isHidden = false
tableView.beginUpdates()
tableView.insertRows(at: [insertIndexPath], with: .automatic)
tableView.endUpdates()
}
}
}
推荐阅读
- mysql - MySQL:选择一组的最后一个 Max() 值,但由另一组汇总
- javascript - 在使用 addEventListener 添加事件之前使用 removeEventListener 删除事件不起作用
- javascript - Excel 公式计算转换为 JavaScript
- apache - 用于镶木地板的 MRUnit
- python-3.x - 你可以重新采样一个没有日期的系列吗?
- autohotkey - 如何获取所选文本的大小(px)和字体粗细?
- javascript - 数组解构和扩展运算符
- python - Cython:将 C 缓冲区内存视图返回给 Python
- html - 使用 object-fit:cover 将图像放在 div 中
- rabbitmq - RabbitMQ 中的密钥感知消费者