yukihiro.swift.py

情報系の大学院生が Swift による iPhone アプリ開発や Python の自然言語処理,Web アプリ開発,ときどき研究ごとについて綴ります.

UITextField の入力内容を UITableView で表示するアプリ 2 / 100 [Swift iPhoneアプリ 100本ノック]

こんばんは!
id:yukihiro1010 です.

今日は,多くのアプリで使用されている UITableView を使ったアプリを開発しました!
このアプリは,UITextField の入力内容を UITableView で表示するシンプルなものです.

今回のソースコードでやっていることは以下の通りです.

  1. 構造体(名前,説明を含む)を定義
  2. UITableView の実装
  3. 名前と説明を入力するための UITextField を定義して,入力内容を取得
  4. その内容をテーブルに追加して表示
  5. テーブルのセルを編集可能にし,削除できるようにした
  6. テーブルの1番下から削除していくボタンを設置
import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var titleField: UITextField!
    @IBOutlet weak var desField: UITextField!
    @IBOutlet weak var itemTable: UITableView!
    
    struct Item {
        var name: String = "no-name"
        var desc: String = "no-desc"
    }
    
    var items: [Item] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        itemTable.delegate = self
        itemTable.dataSource = self
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func addButton() {
        items.append(Item(name: titleField.text, desc: desField.text))
        
        titleField.text = ""
        desField.text = ""
        
        itemTable.reloadData()
    }
    
    @IBAction func removeButton() {
        items.removeLast()
        itemTable.reloadData()
    }
    
    /* 行数を返す関数(必須) */
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    /* 行ごとのセルの中身を設定する関数(必須) */
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle , reuseIdentifier: nil)
        cell.textLabel?.text = items[indexPath.row].name
        cell.detailTextLabel?.text = items[indexPath.row].desc
        
        return cell
    }
    
    /* 編集可能/不可能を指定する関数 */
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    
    /* 移動可能/不可能を指定する関数 */
    func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    
    /* 指定した編集操作後の処理を記述する関数 */
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.Delete) {
            items.removeAtIndex(indexPath.row)
            itemTable.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }
}

f:id:yukihiro1010:20150420204254p:plain

ボタンをクリックすると背景画像が変わるアプリ 1 / 100 [Swift iPhoneアプリ 100本ノック]

もう桜も散ってしまいましたね(´・ω・`)
id:yukihiro1010 です.

Swift の勉強を初めて少し経ちますが,まだまだ慣れてない感じです.
そこで,SwiftiPhone アプリの100本ノックをしていきます!

この思いに至ったのは,下記の記事を見たからですw
継続は力なりですね!
毎日は難しいかも知れませんが…なるべくスパンを空けずに頑張ります!

gigazine.net

さて,では本題です.
今日は,ボタンを押すと背景画像がランダムで変わるアプリを開発しました!
コードの流れは単純です.

  1. 画像の名前を配列として持つ
  2. ボタンを押すと,ランダムに整数が生成される
  3. ランダム数を添え字として画像の名前を取り出し,読み込んで表示

以下にソースコードとアプリの動作画像を掲載しておきます!

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var backgroundImage: UIImageView!

    var imageStrings: [String] = ["dog1.jpg", "dog2.jpg", "dog3.jpg", "dog4.jpg"]

    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.[
    }

    @IBAction func backImage() {
        var randomInt = Int(arc4random_uniform(UInt32(imageStrings.count)))
        backgroundImage.image = UIImage(named: imageStrings[randomInt])
    }
}

f:id:yukihiro1010:20150419170916p:plainf:id:yukihiro1010:20150419170922p:plain

ごあいさつ

はじめまして!

id:yukihiro1010 です.

 

当ブログは,SwiftPython で作った(作る)アプリの開発日誌や備忘録です.

あと,それらに関連する情報についても綴っていきます.

 

 文章書くのは正直苦手(笑)なのですが,

気長にやっていこうと思いますので,よろしくお願い致しますm(_ _)m