【发布时间】:2019-08-17 15:38:17
【问题描述】:
我在 UITableView 中创建了分组部分,但值变得重复。如何填充每个部分下的项目?我已经创建的部分。少数 Title 项为空。
SectionList --> 标题 --> 项目
点赞:
- Bir 有一项
- 项目计划有空项目
Proj Ev 有三个项目
我想在每个部分的标题中显示 textField。
代码:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.dataSource = self
tableView.delegate = self
if let url = Bundle.main.url(forResource: "AppD", withExtension: "json") {
do {
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
let jsonData = try decoder.decode(AppointmentDetail.self, from: data)
self.AppData = jsonData
self.tableView.reloadData()
} catch {
print("error:\(error)")
}
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return AppData?.sectionList?[section].title
}
func numberOfSections(in tableView: UITableView) -> Int {
return AppData?.sectionList?.count ?? 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return AppData?.sectionList?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
// Configure the cell...
if let sections = AppData?.sectionList?[indexPath.section].items {
for item in sections {
if item.textField != "" {
cell.textLabel?.text = item.textField
}
}
}
【问题讨论】:
-
您不能在
numberOfSections和numberOfRowsInSection中使用同一行代码。在cellForRowAt中使用循环是没有意义的。您需要获取特定于给定索引路径的一个值。 -
@rmaddy 我该怎么做你能指导我吗?我尝试了不同的方式,但没有真正实现。因为标题是部分,项目是值。
标签: ios swift uitableview tableview