Skip to content

Instantly share code, notes, and snippets.

@ochim
Created June 11, 2017 14:42
Show Gist options
  • Select an option

  • Save ochim/a9f861bf80753ddbcda37f31e36b6a43 to your computer and use it in GitHub Desktop.

Select an option

Save ochim/a9f861bf80753ddbcda37f31e36b6a43 to your computer and use it in GitHub Desktop.
[Swift3]UIViewControllerにUITableViewを実装するサンプル
import UIKit
 
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
 
    // ステータスバーの高さ
    let statusBarHeight = UIApplication.shared.statusBarFrame.height
     
    override func viewDidLoad() {
        super.viewDidLoad()
         
        // UITableView を作成
        let tableView = UITableView()
         
        // サイズと位置調整
        tableView.frame = CGRect(
            x: 0,
            y: statusBarHeight,
            width: self.view.frame.width,
            height: self.view.frame.height - statusBarHeight
        )
                 
        // Delegate設定
        tableView.delegate = self
         
        // DataSource設定
        tableView.dataSource = self
 
        // 画面に UITableView を追加
        self.view.addSubview(tableView)
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
     
    // MARK: - UITableViewDataSource
     
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // セルを作る
         
        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
        cell.accessoryType = .detailButton
        cell.textLabel?.text = "セル\(indexPath.row + 1)"
        cell.detailTextLabel?.text = "\(indexPath.row + 1)番目のセルの説明"
         
        return cell
    }
     
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // セルの数を設定
        return 5
    }
     
    // MARK: - UITableViewDelegate
     
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // セルがタップされた時の処理
        print("タップされたセルのindex番号: \(indexPath.row)")
    }
     
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        // セルの高さを設定
        return 64
    }
     
    func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
        // アクセサリボタン(セルの右にあるボタン)がタップされた時の処理
        print("タップされたアクセサリがあるセルのindex番号: \(indexPath.row)")
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment