如何使用UICollectionView在单元格左侧创建部分和标题?

时间:2016-04-29 05:33:51

标签: ios swift uicollectionview uitableviewsectionheader

我不确定,如果我把问题写得相当好。如果你考虑改进它,那就去做吧:)但这就是我的意思:

enter image description here

  • 每个橙色框都是新部分框的标题(宽度,高度是静态的)。
  • 这将链接到带有部分
  • 的获取结果控制器
  • 黄色单元格始终占据宽度的其余部分,并具有高度的自动尺寸(取决于文本的长度)。

请告诉我:

  1. 是否可以使用UICollectionView完成此操作?
  2. 如何为黄色单元格安排自动高度尺寸?
  3. 如何漂浮黄色,就像在图像上一样?

2 个答案:

答案 0 :(得分:2)

我认为你可以使用UITableViewController做类似的事情。

enter image description here

这个想法是:

  • 将单元格自定义为左右两部分。
  • 部分第一项的左侧用于部分标题,请注意Clip SubviewsCellView的{​​{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来做。相应地调整其他单元格的大小。