swift2 UICollectionView页眉和页脚视图

ryoqjall  于 2022-11-06  发布在  Swift
关注(0)|答案(5)|浏览(205)

你好,我想知道我们如何实现这一点?
UITableView中,我们可以简单地

tableView.tableHeaderView = SomeView;

tableView.tableFooterView = SomeView;

但是我想知道如何对UICollectionView做同样的事情。
P.S.:章节页眉和页脚

ct2axkht

ct2axkht1#

UITableView有全局头文件tableHeaderViewtableFooterView,但是UICollectionView没有全局头文件,因此您必须使用节头文件。

nr7wwzry

nr7wwzry2#

试试看...
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")
    }
}

我希望它能帮上忙...

wlsrxk51

wlsrxk513#

使用者区段Header和Footer,并建立该区段的类别,例如headerFooter:UICollectionReusableView然后在这个类中编写你想要的代码,然后在视图控制器中编写如下代码:

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
}

对于视图类:

class CollectionReusableView: UICollectionReusableView {

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

cnh2zyt34#

最简单的方法是设置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)

注意。在集合视图中不能使用自动布局来排列标题。所以使用好的旧手动布局。并且当集合视图改变框架时-你应该重置标题的宽度

bq3bfh9z

bq3bfh9z5#

我明白了。很简单,就像这样。

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;
       }
}

相关问题