LoginSignup
123
120

More than 5 years have passed since last update.

SwiftでUIViewController+UITableView

Last updated at Posted at 2014-06-03

こちらの記事がTLに流れてきました。

同じようなことを自分もやったのでメモを残します。
UITableViewの実装をSwiftでやってみた話です。

ViewController.swift

StoryboardにUITableViewを貼り付けて、datasourcedelegateを繋いだ状態。

配列 texts の中身の文字列をセルに表示するところまで。

ViewController.swift
import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView : UITableView

    var texts = ["hello", "world", "hello", "Swift"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int  {
        return texts.count
    }

    func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
        cell.text = texts[indexPath.row]
        return cell
    }

    func tableView(tableView: UITableView?, didSelectRowAtIndexPath indexPath:NSIndexPath!) {
        var text: String = texts[indexPath.row]
        println(text)
    }

}

こんな感じで書けます。

補完がなんか気持ち悪い感じで気持ち悪かった…

123
120
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
123
120