UITableView - テーブルを表示
説明
UITableViewはUIScrollViewのサブクラス
ソースコード
UITableViewSample.swift
import UIKit class UITableViewSample: UIView { override init(frame: CGRect) { super.init(frame: frame) self.backgroundColor = colorPattern.back() let tv = MYTableView(frame: CGRectMake(10, 40, self.frame.width-20, self.frame.height-40)) self.addSubview(tv) tv.layer.masksToBounds = true tv.layer.cornerRadius = 10.0 // 要素追加 for i in 0..<30 { tv.append("ITEM \(i)", detail: "detail\(i)") } } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
MYTableView.swift
import UIKit class MYTableView: UITableView, UITableViewDataSource, UITableViewDelegate { var numberOfCells = 0 var items: [(title: String, detail: String)] = [] var selected: Int? override init(frame: CGRect) { super.init(frame: frame) } override init(frame: CGRect, style: UITableViewStyle) { super.init(frame: frame, style: style) self.registerClass(MYTableViewCell.self, forCellReuseIdentifier: "cell") self.dataSource = self self.delegate = self } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } // 要素を追加 func append(title: String, detail: String) { items.append((title: title, detail: detail)) numberOfCells++ } // 最後の要素を削除 func removeLast() { items.removeLast() numberOfCells-- } // index番目(0-origin)の要素を削除 func removeAtIndex(index: Int) { items.removeAtIndex(index) numberOfCells-- } // セルが選ばれた時 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { println("selected row: \(indexPath.row), value: \(items[indexPath.row])") selected = indexPath.row } // セルの総数 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return numberOfCells } // セルの追加 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell cell.textLabel!.text = items[indexPath.row].title cell.detailTextLabel?.text = items[indexPath.row].detail return cell } }
MYTableViewCell.swift
import UIKit class MYTableViewCell: UITableViewCell { override init(style: UITableViewCellStyle, reuseIdentifier: String?) { // 再利用時,引数のstyleにUITableViewCellStyle.Defaultが // 入るようハードコーディングされているらしい // (http://stackoverflow.com/questions/13174972/ // setting-style-of-uitableviewcell-when-using-ios-6-uitableview-dequeuereusablecel) // スタイルはDefault, Value1, Velue2, Subtitleがある super.init(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier) self.textLabel?.textColor = colorPattern.accent() } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
ViewController.swift
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = colorPattern.back() self.view.addSubview(UITableViewSample(frame: self.view.frame)) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }