Ios 在Tableview中偏移HeaderView,以便像卡片一样显示
我目前正在尝试为UITableView偏移HeaderView,以获得一些存折视图的感觉,但不需要像存折一样操作,只需要上部视图偏移某些像素以覆盖部分 下面是我一直在研究的几个例子。Ios 在Tableview中偏移HeaderView,以便像卡片一样显示,ios,objective-c,swift,swift3,Ios,Objective C,Swift,Swift3,我目前正在尝试为UITableView偏移HeaderView,以获得一些存折视图的感觉,但不需要像存折一样操作,只需要上部视图偏移某些像素以覆盖部分 下面是我一直在研究的几个例子。 如果您注意到第一张图片,那么布局是在CollectionView中完成定制的,但是,我觉得它的设计过于复杂,无法在UICollectionView中获得基于UITableView的感觉。但是,输出的上下文需要根据第二张图片呈现,就像UITableView中的上下文一样,但我很好奇是否有任何方法可以偏移“y”值以重
如果您注意到第一张图片,那么布局是在CollectionView中完成定制的,但是,我觉得它的设计过于复杂,无法在UICollectionView中获得基于UITableView的感觉。但是,输出的上下文需要根据第二张图片呈现,就像UITableView中的上下文一样,但我很好奇是否有任何方法可以偏移“y”值以重叠UITableView中的上一节标题?您需要使用自定义的
UICollectionViewLayout
。
基本上,使用带有垂直滚动的UICollectionView
,将项目宽度设置为屏幕宽度,并实现自定义UICollectionViewLayout
,而不是使用UICollectionViewFlowLayout
您不能在UITableView
中重叠两个单元格,因为不存在布局的概念。UITableView
中的单元格按顺序排列
一个可能的库。正如@Fry在他的回答中提到的,
UITableView
并不真正希望您“移动”节头或单元格,UICollectionView
可能是一个更好的选择,因为它提供了高度的灵活性(当然,您需要一些工作来实现自定义的UICollectionViewLayout
)
但是,如果您坚持使用UITableView
,这里有一个简单的想法来实现您的要求。只需设置下一节标题的backgroundColor
,以匹配上一节。这实际上只是给你存折视图的“感觉”,而不是它的行为
比如说,
let mockData = [(key: "section 1", value: [], color: UIColor.red),
(key: "section 2", value: [], color: UIColor.blue),
(key: "section 3", value: [3, 4, 5], color: UIColor.green)]
...
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as! TestViewHeader
header.containerBackground.backgroundColor = mockData[section].color
// here .white is just an example, you can add any color you want to match with the background or the navigationBar, depends on your design
header.contentView.backgroundColor = (section == 0) ? .white : mockData[section - 1].color
header.containerLabel.text = mockData[section].key
header.containerBackground.roundCorners(corners: [.topLeft, .topRight])
return header
}
结果如下:
还附上了“我的tableView”标题和“我的助手”功能,供您参考:
您的意思是希望节标题与其上一节标题重叠吗?是的,我的意思是。