前提・実現したいこと
・AssetCatalogに格納した画像のpathをinfo.plistに格納し、UITableViewで表示したい。
※もしこの方法が非効率であり、効率的な方法があるのであればご教授いただければ幸いです。
発生している問題・エラーメッセージ
・AssetCatalogに格納した画像をUITableViewに表示することはできたが、
現在のコードでは直接呼び出しているので、全て同じ画像(MickeyIcon)になっている。
該当のソースコード
swift
1import UIKit 2 3class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource{ 4 var _items:NSArray = [] 5 6 override func viewDidLoad() { 7 super.viewDidLoad() 8 9 // Plistファイルパス 10 let path = Bundle.main.path(forResource: "DisneyCharacterList", ofType:"plist") 11 _items = NSArray(contentsOfFile:path!)! 12 //println(_items); 13 } 14 15 // 設定 16 func numberOfSectionsInTableView(tableView: UITableView) -> Int { 17 return 1 18 } 19 20 // 設定(行数) 21 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) 22 -> Int { 23 return _items.count 24 } 25 26 // 設定(セル) 27 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 28 29 let cell = tableView.dequeueReusableCell(withIdentifier: "DisneyCell", for: indexPath as IndexPath) 30 let dic = _items.object(at: indexPath.row) as! NSDictionary 31 cell.textLabel!.text = dic.value(forKey: "Name") as? String 32 cell.imageView?.image = UIImage(named: "0.MickeyIcon") 33 34 return cell 35 } 36 //セルタップ時にセルの内容を取得する 37 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 38 let cell = tableView.dequeueReusableCell(withIdentifier: "DisneyCell", for: indexPath as IndexPath) 39 let dic = _items.object(at: indexPath.row) as! NSDictionary 40 cell.textLabel!.text = dic.value(forKey: "Name") as? String 41 print(cell.textLabel!.text as Any) 42 } 43}
info.Plist
このPlistに「image」みたいなカラムを追加して、
AssetCataloogの画像のpathを書くイメージ
info.Plist
1 2<?xml version="1.0" encoding="UTF-8"?> 3<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 4<plist version="1.0"> 5<array> 6 <dict> 7 <key>Name</key> 8 <string>Mickey</string> 9 </dict> 10 <dict> 11 <key>Name</key> 12 <string>Minnie</string> 13 </dict> 14 <dict> 15 <key>Name</key> 16 <string>Donald</string> 17 </dict> 18 <dict> 19 <key>Name</key> 20 <string>Goofy</string> 21 </dict> 22 <dict> 23 <key>Name</key> 24 <string>Pluto</string> 25 </dict> 26</array> 27</plist> 28
試したこと
自分の調べ方が悪いのか、該当する記事がみつからず、
他にできることがないか調べている最中です…
その他
セルタップ時にセルの内容を取得するメソッドの中身ってもっとスマートになりませんかね…
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/16 05:24 編集
2020/08/16 09:44
2020/08/16 12:52
2020/08/17 06:18