标签:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
import UIKitclass FaviconTableViewCell: UITableViewCell { //此操作队列运行下载的完成处理器 // var operationQueue:NSOperationQueue? //此单元格显示的URL var url:NSURL? { //当URL发生变化 didSet { //显示此文本 self.textLabel?.text = self.url?.host //创建请求 let request = NSURLRequest(URL: self.url!) let session = NSURLSession.sharedSession() let dataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) -> Void in //将获取到的数据转化成图像 let image = UIImage(data: data!) //对UI的更新必须在主队列上完成 NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in //将已加载的图像赋予图像视图 self.imageView?.image = image //图像视图可能已经因为新图像而改变了尺寸 //所以需要重新调整单元格的布局 self.setNeedsLayout() }) }) as NSURLSessionTask //使用resume方法启动任务 dataTask.resume() } } override func awakeFromNib() { super.awakeFromNib() } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import UIKitclass ViewController: UIViewController { let hosts = ["hangge.com","163.com","baidu.com","qq.com","taobao.com"] override func viewDidLoad() { super.viewDidLoad() } //在本例中,只有一个分区 func numberOfSectionsInTableView(tableView: UITableView!) -> Int { return 1; } //返回表格行数(也就是返回控件数) func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.hosts.count } //创建各单元显示内容(创建参数indexPath指定的单元) func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //为了提供表格显示性能,已创建完成的单元需重复使用 let identify:String = "FaviconCell" //同一形式的单元格重复使用,在声明时已注册 let cell = tableView.dequeueReusableCellWithIdentifier(identify) as! FaviconTableViewCell let host = hosts[indexPath.row] cell.url = url return cell } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }} |
Swift - 异步加载各网站的favicon图标,并在单元格中显示
标签:
原文地址:http://www.cnblogs.com/Free-Thinker/p/4858374.html