12.

多線程加載網(wǎng)絡(luò)圖片

let url = URL(string: "http://c.hiphotos.baidu.com/zhidao/pic/item/5ab5c9ea15ce36d3c704f35538f33a87e950b156.jpg")
        let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
            guard let _ = data,error == nil else { return }
            //回到主線程
            DispatchQueue.main.sync {
                self.headView.image = UIImage(data: data!)
            }
        }
        task.resume()

<li><h1>學(xué)習(xí)代碼:<h1></li>

ViewController.swift

import UIKit

let YHRect = UIScreen.main.bounds
let YHHeight = YHRect.size.height
let YHWidth = YHRect.size.width

let HeadViewHeight = YHHeight/3.0

class ViewController: UIViewController {

    
    let datas = ["下","拉","可","以","出","現(xiàn)","很","神","奇","的","事","情","yo","yo","yo","yo","yo","yo"]
    let tableView = UITableView(frame: YHRect, style: .plain)
    let resueIdentifer = "CustomCell"
    let headView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: YHWidth, height: HeadViewHeight))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    
    func setupView() {
        self.automaticallyAdjustsScrollViewInsets = false
        
        headView.backgroundColor = .white
        headView.contentMode = .scaleAspectFill
        headView.clipsToBounds = true
        view.addSubview(headView)
        
        //加載圖片
        let url = URL(string: "http://c.hiphotos.baidu.com/zhidao/pic/item/5ab5c9ea15ce36d3c704f35538f33a87e950b156.jpg")
        let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
            guard let _ = data,error == nil else { return }
            //回到主線程
            DispatchQueue.main.sync {
                self.headView.image = UIImage(data: data!)
            }
        }
        task.resume()
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: resueIdentifer)
        tableView.showsVerticalScrollIndicator = false
        //下面兩句必不可少崎弃,否則會(huì)出現(xiàn)第一次加載時(shí)位置不對(duì)的情況
        tableView.contentInset.top = HeadViewHeight
        tableView.contentOffset = CGPoint(x: 0.0, y: -HeadViewHeight)
//        tableView.scrollIndicatorInsets.top = HeadViewHeight//右邊指示器的位置
        view.addSubview(tableView)
        view.sendSubview(toBack: tableView)
    }

}

extension ViewController:UITableViewDataSource, UITableViewDelegate {
    //MARK: - DataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: resueIdentifer, for: indexPath)
        cell.textLabel?.text = datas[indexPath.row]
        cell.textLabel?.textAlignment = .center
        return cell
    }
    
    //MARK: - Delegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsety = scrollView.contentOffset.y + scrollView.contentInset.top
        if offsety <= 0 {
            headView.frame = CGRect(x: 0.0, y: 0.0, width: YHWidth, height: HeadViewHeight-offsety)
        }else {
            let height = (HeadViewHeight-offsety) <= 0.0 ? 0.0 : (HeadViewHeight-offsety)
            headView.frame = CGRect(x: 0.0, y: 0.0, width: YHWidth, height: height)
            headView.alpha = height/HeadViewHeight
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市琅绅,隨后出現(xiàn)的幾起案子逆济,更是在濱河造成了極大的恐慌羞反,老刑警劉巖偎肃,帶你破解...
    沈念sama閱讀 216,651評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件科吭,死亡現(xiàn)場(chǎng)離奇詭異昏滴,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)对人,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門谣殊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人牺弄,你說(shuō)我怎么就攤上這事姻几。” “怎么了势告?”我有些...
    開封第一講書人閱讀 162,931評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵蛇捌,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我咱台,道長(zhǎng)络拌,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,218評(píng)論 1 292
  • 正文 為了忘掉前任回溺,我火速辦了婚禮春贸,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘遗遵。我一直安慰自己萍恕,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評(píng)論 6 388
  • 文/花漫 我一把揭開白布瓮恭。 她就那樣靜靜地躺著雄坪,像睡著了一般。 火紅的嫁衣襯著肌膚如雪屯蹦。 梳的紋絲不亂的頭發(fā)上维哈,一...
    開封第一講書人閱讀 51,198評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音登澜,去河邊找鬼阔挠。 笑死,一個(gè)胖子當(dāng)著我的面吹牛脑蠕,可吹牛的內(nèi)容都是我干的购撼。 我是一名探鬼主播跪削,決...
    沈念sama閱讀 40,084評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼迂求!你這毒婦竟也來(lái)了碾盐?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,926評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤揩局,失蹤者是張志新(化名)和其女友劉穎毫玖,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凌盯,經(jīng)...
    沈念sama閱讀 45,341評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡付枫,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了驰怎。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片阐滩。...
    茶點(diǎn)故事閱讀 39,731評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖县忌,靈堂內(nèi)的尸體忽然破棺而出掂榔,到底是詐尸還是另有隱情,我是刑警寧澤芹枷,帶...
    沈念sama閱讀 35,430評(píng)論 5 343
  • 正文 年R本政府宣布衅疙,位于F島的核電站,受9級(jí)特大地震影響鸳慈,放射性物質(zhì)發(fā)生泄漏饱溢。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評(píng)論 3 326
  • 文/蒙蒙 一走芋、第九天 我趴在偏房一處隱蔽的房頂上張望绩郎。 院中可真熱鬧,春花似錦翁逞、人聲如沸肋杖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)状植。三九已至,卻和暖如春怨喘,著一層夾襖步出監(jiān)牢的瞬間津畸,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工必怜, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留肉拓,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,743評(píng)論 2 368
  • 正文 我出身青樓梳庆,卻偏偏與公主長(zhǎng)得像暖途,于是被迫代替她去往敵國(guó)和親卑惜。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評(píng)論 2 354

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