UITableView swift IOS分组部分中的重复值
Posted
技术标签:
【中文标题】UITableView swift IOS分组部分中的重复值【英文标题】:Duplicated values in grouped section UITableView swift IOS 【发布时间】: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 我该怎么做你能指导我吗?我尝试了不同的方式,但没有真正实现。因为标题是部分,项目是值。
【参考方案1】:
进行如下更改
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return AppData?.sectionList?[section].items?.count ?? 0
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
cell.textLabel?.text = AppData?.sectionList?[indexPath.section].items?[indexPath.row].textField
cell.textLabel?.sizeToFit()
cell.textLabel?.numberOfLines = 0
return cell
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
return UITableViewAutomaticDimension
用于将 HeaderView XIB 添加到表视图
var tableHeaderViewObj : BookNowHotelDetailHeader?
inViewdidload
tableHeaderViewObj = BookNowHotelDetailHeader(frame: CGRect(x: 0.0, y: 0.0, width: (window?.frame.width)!, height: 350))
self.BookNowTV.tableHeaderView = tableHeaderViewObj
tableHeaderViewObj?.parentVC = self
tableHeaderViewObj?.UpdateBookNowHotelData(Obj: hotelDetailObj ?? HotelDetailModal())
【讨论】:
在 indexPath.row = 0 上使用第一行和 indexPath.row == AppData?.sectionList?[indexPath.section].items?.count - 1 来获取最后一行然后应用拐角半径。还将表格视图从普通更改为分组。 谢谢。让我应用这些设置。 完成了,谢谢。但是如果您知道,需要知道单元格间距问题吗? 只是一个小问题,实际上你在 groupTableRoundedCorner 方面帮助了我很多。我设计得很完美并且可以正常工作。是否有可能我可以在我的部分组表顶部但在同一个表视图中添加 xib/nib 文件?我正在尝试,但其中的显示问题设置不正确。 @ 是的,我已经添加了,但它出现在每个部分的标题上。但我只需要一次,但就像之前的部分一样,它必须是 tableview 的一部分。以上是关于UITableView swift IOS分组部分中的重复值的主要内容,如果未能解决你的问题,请参考以下文章
IOS 7:从分组样式 UITableView 的一部分中删除边框
在 iOS5 故事板中使用静态和动态内容分组 UITableView