我不确定,如果我把问题写得相当好。如果你考虑改进它,那就去做吧:)但这就是我的意思:
请告诉我:
UICollectionView
完成此操作?答案 0 :(得分:2)
我认为你可以使用UITableViewController做类似的事情。
这个想法是:
Clip Subviews
和CellView
的{{1}}应取消选中。以下是示例代码:
ContentView
<强>更新强> 实际上上面的解决方案中存在一个错误,当滚动到顶部直到第一部分消失,然后你会看到它被重新渲染并且布局将被破坏。
有一种更好的方法可以做到这一点。
将一个内部视图(橙色)添加到headerView中,该视图将通过tableCells呈现。请记住设置标题的高度&gt;在这种情况下,单元格只需要包含正确的部分(黄色)。 不过,您需要调整最后一项的高度。
示例代码:
class ViewController: UITableViewController {
struct Item {
var height: CGFloat = 0
}
var itemDic: [Int: [Item]]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
itemDic = [0 : [Item(height: 10), Item(height: 30), Item(height: 50)],
1 : [Item(height: 20), Item(height: 10)],
2 : [Item(height: 40), Item(height: 30), Item(height: 20)]]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return itemDic.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemDic[section]!.count
}
let sectionHeaderHeight: CGFloat = 100
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let section = indexPath.section
let row = indexPath.row
let items = itemDic[section]!
var height = items[row].height
if (row == items.count - 1) {
var total: CGFloat = 0
for i in items {
total += i.height + 10
}
if(sectionHeaderHeight + 10 > total){
height += (sectionHeaderHeight + 10 - total)
}
}
return height + 10
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let section = indexPath.section
let row = indexPath.row
let items = itemDic[section]!
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")!
let orangeView = cell.viewWithTag(100)!
let yellowView = cell.viewWithTag(101)!
if(row == 0){
let orangeFrame = orangeView.frame
orangeView.frame = CGRect(origin: orangeFrame.origin, size: CGSize(width: orangeFrame.width, height: sectionHeaderHeight))
}
let yellowFrame = yellowView.frame
let height = items[row].height
yellowView.frame = CGRect(origin: yellowFrame.origin, size: CGSize(width: yellowFrame.width, height: height))
return cell
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let orangeView = cell.viewWithTag(100)!
let yellowView = cell.viewWithTag(101)!
let orangeFrame = orangeView.frame
print(orangeFrame)
let yellowFrame = yellowView.frame
print(yellowFrame)
}
}
答案 1 :(得分:0)
我不认为你可以做那个部分标题。但是你用 collectionview cell来做。相应地调整其他单元格的大小。