swift 網(wǎng)格 請求數(shù)據(jù)

(1) ?在viewcontroller里創(chuàng)建網(wǎng)格

import UIKit

class ViewController: UIViewController ,UICollectionViewDelegate,UICollectionViewDataSource{


? ? var readID = "readID"


? ? var flowlayout = UICollectionViewFlowLayout()


? ? var collection: UICollectionView?


? ? var collecArr=[News]()


? ? //? ? var collecArr = ["3","1","2"]




? ? override func viewDidLoad() {


? ? ? ? super.viewDidLoad()


? ? ? ? let urlStr = "http://api.jisuapi.com/news/get"


? ? ? ? let par : [String:Any] = [


? ? ? ? ? ? "channel" : "頭條",

? ? ? ? ? ? "appkey" ? : "de394933e1a3e2db"

? ? ? ? ]




? ? ? ? NetworkTools.sharedInstance.request(.GET, urlString: urlStr, parameters: par) { (result, error) in


? ? ? ? ? ? guard error == nil else{


? ? ? ? ? ? ? ? return

? ? ? ? ? ? }


? ? ? ? ? ? guard let jsonDict = result else{

? ? ? ? ? ? ? ? return

? ? ? ? ? ? }


? ? ? ? ? ? let dict = jsonDict as! NSDictionary

? ? ? ? ? ? let resultDict = dict.value(forKey: "result") as! NSDictionary

? ? ? ? ? ? let listArray = resultDict.value(forKey: "list") as! NSArray



? ? ? ? ? ? for item in listArray{


? ? ? ? ? ? ? ? let dic = item as! NSDictionary

? ? ? ? ? ? ? ? let oneNew = News()


? ? ? ? ? ? ? ? oneNew.title = dic.value(forKey: "title") as! String

? ? ? ? ? ? ? ? oneNew.content = dic.value(forKey: "content") as! String

? ? ? ? ? ? ? ? oneNew.pic = dic.value(forKey: "pic") as! String

? ? ? ? ? ? ? ? self.collecArr.append(oneNew)


? ? ? ? ? ? }


? ? ? ? ? ? self.collection?.reloadData()


? ? ? ? }



? ? ? ? // 設(shè)置網(wǎng)格的大小


? ? ? ? flowlayout.itemSize = CGSize(width:self.view.frame.size.width/4, height: 100)


? ? ? ? //設(shè)置最小行間距


? ? ? ? flowlayout.minimumLineSpacing = 1

? ? ? ? flowlayout.headerReferenceSize = CGSize(width: self.view.frame.size.width, height: 50)

? ? ? ? flowlayout.footerReferenceSize = CGSize(width: self.view.frame.size.width, height: 50)



? ? ? ? //設(shè)置最小列間距


? ? ? ? flowlayout.minimumInteritemSpacing = 40




? ? ? ? //設(shè)置分區(qū)縮進量


? ? ? ? flowlayout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 20, right: 10)



? ? ? ? // 設(shè)置滾動方向


? ? ? ? flowlayout.scrollDirection = UICollectionViewScrollDirection.vertical


? ? ? ? // 網(wǎng)格對象


? ? ? ? collection = UICollectionView(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height) , collectionViewLayout: flowlayout)


? ? ? ? // 設(shè)置代理協(xié)議


? ? ? ? collection?.delegate = self


? ? ? ? collection?.dataSource = self


?? ? ? ? collection?.backgroundColor = UIColor.white


? ? ? ? collection?.register(NewsCollectionViewCell? .self, forCellWithReuseIdentifier: readID)


? ? ? ? collection?.register(HeaderCollectionReusableView.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")


? ? ? ? collection?.register(FootCollectionReusableView.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "foot")

? ? ? ? // 添加網(wǎng)格


? ? ? ? self.view .addSubview(collection!)


? ? }


? ? // 實現(xiàn)網(wǎng)格的協(xié)議代理


? ? func numberOfSections(in collectionView: UICollectionView) -> Int {


? ? ? ? return 1


? ? }


? ? func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {


? ? ? ? //? ? ? ? return collecArr.count


? ? ? ? return collecArr.count


? ? }




? ? func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


? ? ? ? // 重用cell


? ? ? ? let cell:NewsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: readID, for: indexPath) as! NewsCollectionViewCell


? ? ? ? let model:News = self.collecArr[indexPath.item]

? ? ? ? cell.titleLabel.text = model.title

? ? ? ? cell.imageView.sd_setImage(with: URL(string: model.pic), completed: nil)

? ? ? ? return cell


? ? }


? ? func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView{


? ? ? ? if kind == "UICollectionElementKindSectionHeader" {


? ? ? ? ? ? let head:HeaderCollectionReusableView = collection?.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header", for: indexPath) as! HeaderCollectionReusableView


? ? ? ? ? ? head.label.text = "asdasdqwq"



? ? ? ? ? ? return head


? ? ? ? }

? ? ? ? let foot:FootCollectionReusableView = collection?.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "foot", for: indexPath) as! FootCollectionReusableView


? ? ? ? foot.label.text = "caonima"



? ? ? ? return foot

? ? }

}

(2) 創(chuàng)建自定義cell繼承UIcollectionviewcell

import UIKit

class NewsCollectionViewCell: UICollectionViewCell {


? ? var titleLabel = UILabel()

? ? var imageView = UIImageView()

? ? var button = UIButton()


? ? override init(frame: CGRect) {

? ? ? ? super.init(frame: frame)

? ? ? ? titleLabel.frame = CGRect(x: 0, y: 0, width: self.contentView.frame.size.width, height: self.contentView.frame.size.height * 0.25)

? ? ? ? self.addSubview(titleLabel)


? ? ? ? imageView.frame = CGRect(x: 0, y: titleLabel.frame.size.height, width: self.contentView.frame.size.width, height: self.contentView.frame.size.height - titleLabel.frame.size.height)

? ? ? ? self.addSubview(imageView)


? ? ? ? button.frame = CGRect(x: (imageView.frame.size.width - 40) / 2, y: (imageView.frame.size.height - 40) / 2, width: 40, height: 40)

?? ? ? // button.backgroundColor = .green

?? ? ? ? button.setImage(UIImage(named: "播放"), for: .normal)


? ? ? ? self.imageView.addSubview(button)


? ? }


? ? required init?(coder aDecoder: NSCoder) {

? ? ? ? fatalError("init(coder:) has not been implemented")

? ? }

}

// (3)創(chuàng)建一個繼承于viewcontroller的類用來頭部視圖

import UIKit

class HeaderCollectionReusableView: UICollectionReusableView {


? ? lazy var label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 50))



? ? override init(frame: CGRect) {

? ? ? ? super.init(frame: frame)

? ? ? ? label.backgroundColor = .red

? ? ? ? label.textAlignment = .center

? ? ? ? self.addSubview(label)

? ? }


? ? required init?(coder aDecoder: NSCoder) {

? ? ? ? fatalError("init(coder:) has not been implemented")

? ? }


}

(4) ?創(chuàng)建一個繼承于viewcontroller的類用來做尾部視圖

import UIKit

class FootCollectionReusableView: UICollectionReusableView {


? ? lazy var label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 50))



? ? override init(frame: CGRect) {

? ? ? ? super.init(frame: frame)

? ? ? ? label.backgroundColor = .red

? ? ? ? label.textAlignment = .center

? ? ? ? self.addSubview(label)

? ? }


? ? required init?(coder aDecoder: NSCoder) {

? ? ? ? fatalError("init(coder:) has not been implemented")

? ? }


}

?(5) 創(chuàng)建一個nsobject的類用來做model

import UIKit

class News: NSObject {


? ? var time:String = ""


? ? var title:String = ""


? ? var pic:String = ""


? ? var content:String = ""


? ? var weburl:String = ""

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末岂津,一起剝皮案震驚了整個濱河市屋灌,隨后出現(xiàn)的幾起案子蜡镶,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,123評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件若未,死亡現(xiàn)場離奇詭異沸伏,居然都是意外死亡,警方通過查閱死者的電腦和手機哄尔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,031評論 2 384
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來柠并,“玉大人岭接,你說我怎么就攤上這事【视瑁” “怎么了鸣戴?”我有些...
    開封第一講書人閱讀 156,723評論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長粘拾。 經(jīng)常有香客問我窄锅,道長,這世上最難降的妖魔是什么缰雇? 我笑而不...
    開封第一講書人閱讀 56,357評論 1 283
  • 正文 為了忘掉前任入偷,我火速辦了婚禮,結(jié)果婚禮上械哟,老公的妹妹穿的比我還像新娘疏之。我一直安慰自己,他們只是感情好暇咆,可當我...
    茶點故事閱讀 65,412評論 5 384
  • 文/花漫 我一把揭開白布锋爪。 她就那樣靜靜地躺著,像睡著了一般糯崎。 火紅的嫁衣襯著肌膚如雪几缭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,760評論 1 289
  • 那天沃呢,我揣著相機與錄音年栓,去河邊找鬼。 笑死薄霜,一個胖子當著我的面吹牛某抓,可吹牛的內(nèi)容都是我干的纸兔。 我是一名探鬼主播,決...
    沈念sama閱讀 38,904評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼否副,長吁一口氣:“原來是場噩夢啊……” “哼汉矿!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起备禀,我...
    開封第一講書人閱讀 37,672評論 0 266
  • 序言:老撾萬榮一對情侶失蹤洲拇,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后曲尸,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體赋续,經(jīng)...
    沈念sama閱讀 44,118評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,456評論 2 325
  • 正文 我和宋清朗相戀三年另患,在試婚紗的時候發(fā)現(xiàn)自己被綠了纽乱。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,599評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡昆箕,死狀恐怖鸦列,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情鹏倘,我是刑警寧澤薯嗤,帶...
    沈念sama閱讀 34,264評論 4 328
  • 正文 年R本政府宣布,位于F島的核電站第股,受9級特大地震影響应民,放射性物質(zhì)發(fā)生泄漏话原。R本人自食惡果不足惜夕吻,卻給世界環(huán)境...
    茶點故事閱讀 39,857評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望繁仁。 院中可真熱鬧涉馅,春花似錦、人聲如沸黄虱。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,731評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽捻浦。三九已至晤揣,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間朱灿,已是汗流浹背昧识。 一陣腳步聲響...
    開封第一講書人閱讀 31,956評論 1 264
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留盗扒,地道東北人跪楞。 一個月前我還...
    沈念sama閱讀 46,286評論 2 360
  • 正文 我出身青樓缀去,卻偏偏與公主長得像,于是被迫代替她去往敵國和親甸祭。 傳聞我的和親對象是個殘疾皇子缕碎,可洞房花燭夜當晚...
    茶點故事閱讀 43,465評論 2 348

推薦閱讀更多精彩內(nèi)容