「Ios/swift/TableView/API画像連動」の版間の差分
ナビゲーションに移動
検索に移動
ページの作成:「-ViewController.swift class ViewController: UIViewController ,UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var imgTableView: UITableView! var _img...」 |
編集の要約なし |
||
| 1行目: | 1行目: | ||
==事前準備== | |||
[[ios/swift/TableView]] [ショートカット] | |||
[[ios/swift/外部ライブラリ]] [ショートカット] | |||
==サンプル== | |||
-ViewController.swift | -ViewController.swift | ||
class ViewController: UIViewController ,UITableViewDataSource, UITableViewDelegate { | class ViewController: UIViewController ,UITableViewDataSource, UITableViewDelegate { | ||
2016年6月24日 (金) 22:53時点における版
事前準備
ios/swift/TableView [ショートカット]
ios/swift/外部ライブラリ [ショートカット]
サンプル
-ViewController.swift
class ViewController: UIViewController ,UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var imgTableView: UITableView!
var _imgs: Array<ImgInit> = []
override func viewDidLoad() {
super.viewDidLoad()
imgTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
imgTableView.delegate = self
imgTableView.dataSource = self
Alamofire.request(.GET, "http://matomaker.net/api/testimgs", parameters: ["param1": "value1"]).responseJSON { response in
if response.result.isSuccess {
let jsonDic = response.result.value as! NSDictionary
let imgs = jsonDic["imgs"] as! NSArray
let cnt = jsonDic["cnt"] as! Int
print(cnt)
for img in imgs {
let name:String = img["name"] as! String
let thumbnail:String = img["thumbnail"] as! String
let _img = ImgInit(name: name, thumbnail: thumbnail) as ImgInit
self._imgs.append(_img)
}
self.imgTableView.reloadData() // 更新
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self._imgs.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print(self._imgs[indexPath.row].thumbnail)
let cell =
tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell
cell.setCell(self._imgs[indexPath.row].thumbnail, text: self._imgs[indexPath.row].name)
return cell
}
}
-CustomTableViewCell.swift
import UIKit
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var customLabelView: UILabel!
@IBOutlet weak var customImageView: UIImageView!
func setCell(imgpath: String, text: String) {
customLabelView.numberOfLines = 0; // 複数行
if imgpath.containsString("http://") || imgpath.containsString("https://") {
let imageURL = NSURL(string: imgpath)
customImageView.sd_setImageWithURL(imageURL)
} else {
customImageView.image = UIImage(named:imgpath)
}
customLabelView.text = text
}
}