UICollectionView页眉和页脚视图

时间:2015-10-17 05:51:20

标签: ios swift uiview uicollectionview swift2

您好我想知道我们是如何实现这一目标的?

在UITableView中,我们可以执行tableView.tableHeaderView = SomeView;tableView.tableFooterView = SomeView;

但我想知道如何为UICollectionView做同样的事情。

P.S。:章节页眉和页脚

5 个答案:

答案 0 :(得分:8)

试试吧......

ViewDidLoad中的第一个注册类

registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "Footer")

然后使用此方法

override func collectionView(collectionView: UICollectionView,   viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

  switch kind {

    case UICollectionElementKindSectionHeader:

        let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! UICollectionReusableView

        header = SomeView
        return header

    case UICollectionElementKindSectionFooter:
        let footer = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath) as! UICollectionReusableView

        footer= SomeView 
        return footer

    default:

       print("anything")
    }
}

我希望它有所帮助...

答案 1 :(得分:3)

UITableView有全局标题tableHeaderViewtableFooterView,但UICollectionView没有全局标题,因此您必须使用标题标题。

答案 2 :(得分:0)

用户部分的页眉和页脚,并为此创建一个类,例如headerFooter:UICollectionReusableView,然后在该类中编写所需的代码,然后在viewcontroller中编写以下内容:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let sectionView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "FooterView", for: indexPath) as! CollectionReusableView

    return sectionView
}

//对于View类:

CollectionReusableView类:UICollectionReusableView {

@IBAction func ButtonClick(_ sender: Any) {
    print("Click 1st")
}
@IBAction func ButtonClick2(_ sender: Any) {
    print("Click 2nd")
}    

}

答案 3 :(得分:0)

我明白了。 像这样非常简单。

        CGFloat headerHeight = 100;
        UIView *headerView = ({
            UIView *view = [[UIView alloc] init];
            view.frame = CGRectMake(0, -headerHeight, collectionView.frame.size.width,headerHeight);
            view;
        });
        [collectionView addSubview:headerView];

        CGFloat footerHeight = 100;
        UIView *footerView = ({
            UIView *view = [[UIView alloc] init];
            view.frame = CGRectMake(0, collectionView.frame.size.height, collectionView.frame.size.width, footerHeight);
            view;
        });

        self.footerView = footerView;
        [collectionView addSubview:footerView];
        collectionView.contentInset = UIEdgeInsetsMake(headerHeight, 0, footerHeight, 0);

并添加

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
       if (scrollView.contentSize.height > scrollView.frame.size.height) {
          CGRect frame = self.footerView.frame;
          frame.origin.y = scrollView.contentSize.height;
          self.footerView.frame = frame;
       }
}

答案 4 :(得分:0)

最简单的方法-设置collectionView.contentInset.top = height 并将视图手动添加到collectionView:

let header = UIView(frame: CGRect(x: 0, y: -height, width: collectionView.frame.width, height: height))
// ... customise view, add subviews
collectionView.addSubview(header)

注意。您不能使用自动布局在集合视图中排列标题。因此,请使用良好的旧手动布局。并且当collectionView更改框架时-您应该重置标头的宽度