「Ios/swift/TableView/API画像連動」の版間の差分
ナビゲーションに移動
検索に移動
編集の要約なし |
編集の要約なし |
||
| 10行目: | 10行目: | ||
-ViewController.swift | -ViewController.swift | ||
import UIKit | |||
import Alamofire | |||
class ViewController: UIViewController ,UITableViewDataSource, UITableViewDelegate { | class ViewController: UIViewController ,UITableViewDataSource, UITableViewDelegate { | ||
@IBOutlet weak var imgTableView: UITableView! | @IBOutlet weak var imgTableView: UITableView! | ||
| 47行目: | 49行目: | ||
-CustomTableViewCell.swift | -CustomTableViewCell.swift | ||
import UIKit | import UIKit | ||
import SDWebImage | |||
class CustomTableViewCell: UITableViewCell { | class CustomTableViewCell: UITableViewCell { | ||
@IBOutlet weak var customLabelView: UILabel! | @IBOutlet weak var customLabelView: UILabel! | ||
2016年6月24日 (金) 23:40時点における版
事前準備
以下を事前に見ておく
ios/swift/TableView/画像付き [ショートカット]
ios/swift/外部ライブラリ/Alamofire [ショートカット]
サンプル
画像とテキストのデータ配列を読み込めるAPIを叩いて、その情報をそのままTableViewで表示する
-ViewController.swift
import UIKit
import Alamofire
class ViewController: UIViewController ,UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var imgTableView: UITableView!
private 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://localhost/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
import SDWebImage
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
}
}
-ImgInit.swift
class ImgInit {
var name:String = ""
var thumbnail:String = ""
init(name: String, thumbnail: String) {
self.name = name
self.thumbnail = thumbnail
}
}