Swift3.0 Day04 CollectionView+StaticFunc

其实这个案例还有一些问题没有解决,但是这个案例里面有许多值得分享的知识点和坑,我来一一列举一下,以后会完善一下这个案例

CollectionView的实现##

Collection view的大部分设置我都是在StoryBoard中完成,但是在实际开发中,我们通常会将TableView和CollectionView 的实现作为控制器的扩展写到另外一个类中,但是特别要注意的是,Swift3.0中貌似要求,控制器中使用到的类别中的方法必须是公共方法,同样的在类别中使用到的控制器中的属性也不允许是私有的。下面来看一下它的用法

extension ViewController : UICollectionViewDataSource {
    
    private func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return interests.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InterestCell", for: indexPath) as! ImgCollectionViewCell
        
        cell.interest = self.interests[indexPath.item]
        
        return cell
        
    }
    
}

Static Func###

其实之前一直是有疑问的,之前OC中的类方法在Swift中如何实现,实际上在这里,我们就是通过静态方法Static Func类实现的在本例中,我们还学到了如何在犯法中返回一个数组,代码片段如下

    static func createInterests() -> [Interest]
    {
        return [
            Interest(featuredImage: UIImage(named: "hello")!),
            Interest( featuredImage: UIImage(named: "dudu")!),
            Interest(featuredImage: UIImage(named: "bodyline")!),
            Interest(featuredImage: UIImage(named: "wave")!),
            Interest(featuredImage: UIImage(named: "darkvarder")!),
            Interest(featuredImage: UIImage(named: "hhhhh")!),
        ]
    }

另外,在使用UIImage(named: "wave")!这个房发的时候,我发现像往常那样直接添加在工程根目录下会找不到这个图片,必须将图片放置在Assets.xcassets下才能找到图片,地址请戳

你可能感兴趣的:(Swift3.0 Day04 CollectionView+StaticFunc)