其實(shí)這個(gè)案例還有一些問題沒有解決,但是這個(gè)案例里面有許多值得分享的知識(shí)點(diǎn)和坑柴钻,我來一一列舉一下,以后會(huì)完善一下這個(gè)案例
CollectionView的實(shí)現(xiàn)##
Collection view的大部分設(shè)置我都是在StoryBoard中完成垢粮,但是在實(shí)際開發(fā)中贴届,我們通常會(huì)將TableView和CollectionView 的實(shí)現(xiàn)作為控制器的擴(kuò)展寫到另外一個(gè)類中,但是特別要注意的是蜡吧,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###
其實(shí)之前一直是有疑問的昔善,之前OC中的類方法在Swift中如何實(shí)現(xiàn)元潘,實(shí)際上在這里,我們就是通過靜態(tài)方法Static Func類實(shí)現(xiàn)的在本例中君仆,我們還學(xué)到了如何在犯法中返回一個(gè)數(shù)組翩概,代碼片段如下
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")!這個(gè)房發(fā)的時(shí)候返咱,我發(fā)現(xiàn)像往常那樣直接添加在工程根目錄下會(huì)找不到這個(gè)圖片钥庇,必須將圖片放置在Assets.xcassets下才能找到圖片,地址請(qǐng)戳