Swift-asynchronously loads the Favicon icon for each Web site and displays it in a cell

Source: Internet
Author: User


The following is a simple application in which the individual cells of the table view automatically load the Favicon icons for each Web site and display them. It is mainly to review how to customize cells, the asynchronous loading of pictures in cells, and the use of Didset.
as follows:

Operation steps:(1) First create cell class-Favicontableviewcell.swift
importUIKitclassFaviconTableViewCell: UITableViewCell {// This operation queue runs the download completion processor // var operationQueue: NSOperationQueue? // URL displayed in this cell varurl: NSURL? {// When URL changes didSet {// Display this text self. textLabel? .text = self.url? .host // create request letrequest = NSURLRequest (URL: self.url!) letsession = NSURLSession.sharedSession () letdataTask = session.dataTaskWithRequest (request, completionHandler: ((data, response, error )-> Voidin // Convert the obtained data into an image letimage = UIImage (data: data!) // The UI update must be completed on the main queue NSOperationQueue.mainQueue (). AddOperationWithBlock ((()-> Voidin / / Assign the loaded image to the image view self.imageView? .Image = ima ge // The image view may have changed size due to the new image // so the cell layout needs to be readjusted self.setNeedsLayout ())))) asNSURLSessionTask // Use the resume method to start the task dataTask.resume ()}} overridefuncawakeFromNib ( ) {super.awakeFromNib ()} overridefuncsetSelected (selected: Bool, animated: Bool) {super.setSelected (selected, animated: animated)}}

(2) Add a table view in Main.storyboard and change the number of prototype cells (prototype cells) in table view to 1,selection (select style) to No Selection (3) to select cells, Set the style to basic and change the Indentifier (identifier) to Faviconcell (4) and then the identity of the cell Inspector (Identity Viewer) change to Favicontableviewcell (5) and then control-drag the table view onto the view controller. Add data sources and delegates separately (DataSource and Delegate) (6) The Viewcontroller.swift code is as follows:
importUIKitclassViewController: UIViewController {lethosts = ["hangge.com", "163.com", "baidu.com", "qq.com", "taobao.com"] overridefuncviewDidLoad () {super.viewDidLoad ()} // in In this example, there is only one partition funcnumberOfSectionsInTableView (tableView: UITableView!)-> Int {return1;} // Returns the number of table rows (that is, the number of controls returned) functableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {returnself .hosts.count} // Create the display content of each cell (create the cell specified by the indexPath parameter) functableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCell {// In order to provide table display performance, the completed cell needs to be repeated Use letidentify: String = "FaviconCell" // Cells of the same form are reused, and registered at the time of declaration letcell = tableView.dequeueReusableCellWithIdentifier (identify) as! FaviconTableViewCell l ethost = hosts [indexPath.row] leturl = NSURL (string: "http: // \ (host) /favicon.ico") cell.url = url returncell} overridefuncdidReceiveMemoryWarning () {super.didReceiveMemoryWarning ()}}

Original from: www.hangge.com reprint please keep the original link: http://www.hangge.com/blog/cache/detail_873.html


Swift-asynchronously loads the Favicon icon for each Web site and displays it in a cell


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.