Main.storyboard

平成30年2月23日(金曜日)

LAYOUT関連
スクロールビュー、4面0設定、中に入るビューも同じく0設定とし、長さを幅を設定。

image(from url) with table cell

平成30年2月21日(水曜日)

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell: CustomCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
        
        if let data = tabledata {
            
            let urlString: NSString = data.results[indexPath.row].artworkUrl60 as NSString
            let imgURL: URL = URL(string: urlString as String)!
            do {
                    let imageData = try Data(contentsOf: imgURL as URL)
                    cell.img.image = UIImage(data: imageData)
            } catch {
                    print("Unable to load data: \(error)")
            }
        }
        return cell
    }

参考url
NSURL, URL and NSData, Data

AppDelegate

平成30年2月21日(水曜日)

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
}

					

TableViewController

平成30年2月21日(水曜日)

import UIKit

class TableViewController: UITableViewController {
    //配列fruitsを設定
    let fruits = ["apple", "orange", "melon", "banana", "pineapple"]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

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

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return fruits.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        // セルに表示する値を設定する
        cell.textLabel!.text = fruits[indexPath.row]
        
        return cell
    }
 }

AppDelegate.swift

平成30年2月21日(水曜日)

BASIC -> AppDelegate

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)
    
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
    let initialViewController = storyboard.instantiateViewController(withIdentifier: "TableViewController")
    
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
    
    return true
    }
}

swift4 配列

平成30年2月19日(火曜日)

var a = [1, 2, 3]
for value in a {
  print(value) // 1, 2, 3
}

// インデックス付き
for (i, value) in a.enumerated() {
  print(i) // 0, 1, 2
  print(value) // 1, 2, 3
}

// nil 以外の要素をループ
var a: [String?] = ["1", nil, "2"]
for case let value? in a {
  print(value) // "1", "2"
}