collectionheaderview

来源:互联网 发布:网络卫生直报平台 编辑:程序博客网 时间:2024/06/10 05:11

collectionview不像tableview那样,有tableheaderview

collectionview若想实现collectionviewheaderview就必须实现  viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath方法,通过创建页眉来实现,实现前必须要注册,否侧会crash

设置其他section的高度为0.0001来实现只返回一个页眉

若要修改headrview的属性可以通过代理来实现,但一定要注意,在设置代理人的时候必须将页眉设置成属性,并且将return写在if的外面

        if indexPath.section == 0 {
            self.headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "header", forIndexPath: indexPath) as! ShopHeaderView
            self.headerView.delegate = self
            self.headerView.logoPicture.sd_setImageWithURL(NSURL(string: self.shop_logo), placeholderImage: nil)
            self.headerView.titleLabel.text = self.shop_name
        }
        return self.headerView


这样写看起来挺奇葩,但是貌似只有这样才不会出现紊乱

0 0