我有一个 UITableView,它已添加到 UIScrollView 内,作为视图控制器的一部分,该视图控制器粘附到如下所示的模型的一部分:-
正如您在模型中看到的,UITableView 位于“特色企业”标签和包含绿色和粉色子视图的视图之间
此外,如果观察的话,表格视图中的每个单元格都有一个按钮,单击该按钮会切换包含详细信息列表的卡片视图。卡片视图的高度根据详细信息的数量而变化。
我可以扩展和收缩单元格以显示卡片视图,该视图是表视图单元格的一部分,如模型中所示。
问题在于扩展和收缩父表视图,以便看到该表视图中的所有单元格(表视图不应该是可滚动的,并且任何单元格都不应该被隐藏)并且只有滚动视图是可滚动的。
这是我的表格视图单元格的代码,其中涉及单元格的扩展以及更改表格的尺寸(高度)和滚动视图的内容大小:-
func toggleBusinessesTable() {
var businessTableHeight = self.parent!.businessTable.frame.height
if self.parent!.indexOfCellToExpand > -1 {
self.parent!.businessTable.snp.remakeConstraints { (make) in
make.top.equalTo(self.parent!.featuredBusinessesHeading.snp.bottom).offset(3.5)
make.left.equalTo(self.parent!.scroll.snp.left)
make.width.equalTo(self.parent!.view.bounds.width)
make.height.equalTo(CGFloat(businessTableHeight) + CGFloat(self.tableHeight))
}
self.parent!.scroll.layoutIfNeeded()
self.parent!.scroll.contentSize.height = self.parent!.usersCollectionView.frame.maxY + 75
}
else if self.parent!.indexOfCellToExpand == -1 {
self.parent!.businessTable.snp.remakeConstraints { (make) in
make.top.equalTo(self.parent!.featuredBusinessesHeading.snp.bottom).offset(3.5)
make.left.equalTo(self.parent!.scroll.snp.left)
make.width.equalTo(self.parent!.view.bounds.width)
make.height.equalTo(CGFloat(businessTableHeight) - CGFloat(self.tableHeight))
}
self.parent!.scroll.layoutIfNeeded()
self.parent!.scroll.contentSize.height = self.parent!.usersCollectionView.frame.maxY + 75
}
}
func expandCell(_ sender: UI.Button) {
if self.parent!.indexOfCellToExpand != self.tag {
print("EXPANDING...")
self.parent!.indexOfCellToExpand = self.tag
self.parent!.businessTable.reloadRows(at: [IndexPath(row: self.tag, section: 0)], with: .fade)
self.parent!.businessTable.scrollToRow(at: IndexPath(row: self.tag, section: 0), at: .top, animated: true)
}
else if self.parent!.indexOfCellToExpand == self.tag {
print("CONTRACTING...")
self.parent!.indexOfCellToExpand = -1
self.parent!.businessTable.reloadRows(at: [IndexPath(row: self.tag, section: 0)], with: .fade)
self.parent!.businessTable.scrollToRow(at: IndexPath(row: self.tag, section: 0), at: .top, animated: true)
}
// self.toggleBusinessesTable()
}
The parent
是具有滚动视图和表格视图的视图控制器
The businessTable
UITableView 是有问题的表吗?scroll
是保存表格视图的 UIScrollView
我正在使用表视图单元格的标签来跟踪indexPath
下面的代码计算表视图单元格应该扩展的差异:-
// tableHeight is initially 0 at the beginning
if self.business_service_charges.count <= 4 {
for each in self.business_service_charges {
self.tableHeight += 80 + each.sub_service_charges.count*80
}
} else if self.business_service_charges.count > 4 {
for each in self.business_service_charges[0...3] {
self.tableHeight += 80 + each.sub_service_charges.count*80
}
}
the tableHeight
表视图单元格的变量用于计算表视图应扩展/收缩的程度。
但在执行时self.toggleBusinessesTable()
表格视图的扩展超出了应有的范围,在底部视图和表格视图之间添加了多余的间距,并且在收缩时,表格视图变得更小,隐藏了其他单元格,使表格视图可滚动。
UITableView 大小例程:-
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == indexOfCellToExpand {
var tableHeight = 0
if self.featuredBusinesses[indexPath.row].businessServiceCharges.count <= 4 {
for each in self.featuredBusinesses[indexPath.row].businessServiceCharges {
tableHeight += 80 + each.sub_service_charges.count*80
}
} else if self.featuredBusinesses[indexPath.row].businessServiceCharges.count > 4 {
for each in self.featuredBusinesses[indexPath.row].businessServiceCharges[0...3] {
tableHeight += 80 + each.sub_service_charges.count*80
}
}
return CGFloat(250 + tableHeight)
}
return 250
}
indexOfCellToExpand
是一个变量,用于跟踪已展开的表视图单元格
简而言之,有什么方法可以展开和折叠表格视图并适当更改滚动视图的内容大小以获得模型所需的效果吗?
任何帮助将不胜感激。
另外,我也用过snapkit
设置单元格的布局