UITableViewCellの左端にアイコンを追加する方法
import UIKit class PagingNavController: UIViewController, UITableViewDelegate, UITableViewDataSource { private var myTableView: UITableView! private let myItems: NSArray = ["TEST1", "TEST2", "TEST3"] override func viewDidLoad() { super.viewDidLoad() self.view.addSubview(initTableView()) } func initTableView() -> UITableView { // Status Barの高さを取得する. let barHeight: CGFloat = UIApplication.sharedApplication().statusBarFrame.size.height // Viewの高さと幅を取得する. let displayWidth: CGFloat = self.view.frame.width let displayHeight: CGFloat = self.view.frame.height // TableViewの生成する(status barの高さ分ずらして表示). myTableView = UITableView(frame: CGRect(x: 0, y: profileViewHeight, width: displayWidth, height: displayHeight - profileViewHeight)) // Cell名の登録をおこなう. myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell") // DataSourceの設定をする. myTableView.dataSource = self // Delegateを設定する. myTableView.delegate = self return myTableView } /* Cellが選択された際に呼び出されるデリゲートメソッド. */ func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { print("Num: \(indexPath.row)") print("Value: \(myItems[indexPath.row])") } /* Cellの総数を返すデータソースメソッド. (実装必須) */ func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return myItems.count } /* Cellに値を設定するデータソースメソッド. (実装必須) */ func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // 再利用するCellを取得する. let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) // Cellに値を設定する. cell.textLabel!.text = "\(myItems[indexPath.row])" //Cellの右端に > をつける cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator //画像の追加はこれだけ! cell.imageView!.image = UIImage(named: "sample") //画像の追加はこれだけ! return cell } }
このサイトを参考にしました。
超おすすめ!
006 UITableViewでテーブルを表示 - Swift Docs