Skip to content

Commit 1abc17e

Browse files
author
pixel-ink
committed
UITableView用メソッドの追加
1 parent d15573b commit 1abc17e

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

PIImageCache/PIImageCache/PIImageCache.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ public class PIImageCache {
6565
}
6666
}
6767
}
68+
69+
public func getWithId(url: NSURL, id: Int, then: (id: Int, image: UIImage?) -> Void) {
70+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
71+
[weak self] in
72+
let image = self?.get(url)
73+
dispatch_async(dispatch_get_main_queue()) {
74+
then(id: id, image: image)
75+
}
76+
}
77+
}
6878

6979
public func prefetch(urls: [NSURL]) {
7080
for url in urls {

PIImageCache/PIImageCache/TableViewController.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import UIKit
44
class TableViewCell : UITableViewCell {
55
@IBOutlet weak var icon: UIImageView!
66
@IBOutlet weak var body: UILabel!
7+
var id:Int!
78
}
89

910
class TableViewController : UITableViewController {
@@ -19,8 +20,14 @@ class TableViewController : UITableViewController {
1920
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
2021
let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
2122
let i = indexPath.row
23+
cell.id = i
2224
let url = NSURL(string: "http://lorempixel.com/200/200/" + lormpixelCategory[i] )!
23-
cell.icon.imageOfURL(url)
25+
PIImageCache.shared.getWithId(url, id: i) {
26+
[weak self] id, image in
27+
if id == cell.id {
28+
cell.icon.image = image
29+
}
30+
}
2431
cell.body.text = lormpixelCategory[i]
2532
return cell
2633
}

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,25 @@ let cache = PIImageCache.shared
8686
cache.prefetch(url)
8787
```
8888

89+
### downloadWithId
90+
91+
set id, callback image with the id.
92+
93+
useful for UITableViewCell
94+
95+
```
96+
// example: code in cellForRowAtIndexPath
97+
let url = NSURL(string: "http://lorempixel.com/200/200/" )!
98+
let id = indexPath.row
99+
cell.id = indexPath.row
100+
PIImageCache.shared.getWithId(url, id: i) {
101+
[weak self] id, image in
102+
if id == cell.id {
103+
cell.icon.image = image
104+
}
105+
}
106+
```
107+
89108
### configurable
90109

91110
```Config.swift

0 commit comments

Comments
 (0)