<file_sep>//
//  RecordTableViewCell.swift
//  1_Test_TableView_CollectionView
//
//  Created by 1 on 2020/10/27.
//

import UIKit

class RecordTableViewCell: UITableViewCell {
    
    static var nib: UINib {
        return UINib(nibName: "RecordTableViewCell", bundle: Bundle(for: self))
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
<file_sep>//
//  RecordTableViewController.swift
//  1_Test_TableView_CollectionView
//
//  Created by 1 on 2020/10/27.
//

import UIKit

class RecordTableViewController: UITableViewController {
    
    var recordListInTableView: [String] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(RecordTableViewCell.nib, forCellReuseIdentifier: "RecordTableViewCell")
        
    }


    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return recordListInTableView.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let tablecell = tableView.dequeueReusableCell(withIdentifier: "RecordTableViewCell", for: indexPath) as! RecordTableViewCell
        tablecell.textLabel?.text = recordListInTableView[indexPath.row]

        return tablecell
    }
}
<file_sep>//
//  ViewController.swift
//  Test_Collection
//
//  Created by 1 on 2020/10/28.
//

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource {

    
    var mycollectionView: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()
        makeMyCollectionView()
        
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        mycollectionView.collectionViewLayout.invalidateLayout()
    }
    
    func makeMyCollectionView() {
        let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        
        // section與 section 之間的距離 (如果只有一個section, 可以想像成 frame)
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
        //cell的寬、高
        layout.itemSize = CGSize(width: (self.view.frame.size.width - 30) / 2, height: 120)
        // 滑動方向為「垂直」的話即「上下」的間距;滑動方向為「平行」則為「左右」的間距
        layout.minimumLineSpacing = CGFloat(integerLiteral: 10)
        // 滑動方向為「垂直」的話即「左右」的間距;滑動方向為「平行」則為「上下」的間距
        layout.minimumInteritemSpacing = CGFloat(integerLiteral: 10)
        //滑動方向預設為垂直。注意若設為垂直，則cell的加入方式為由左至右，滿了才會換行；若是水平則由上往下，滿了才會換列
        //layout.scrollDirection = UICollectionView.ScrollDirection.vertical
        
        self.mycollectionView = UICollectionView(frame: CGRect(x: 0, y: 10, width: self.view.frame.width, height: self.view.frame.size.height), collectionViewLayout: layout)
        self.mycollectionView.dataSource = self
        self.mycollectionView.delegate = self
        self.mycollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
        self.mycollectionView.backgroundColor = UIColor.white
        self.view.addSubview(mycollectionView)
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
        cell.backgroundColor = indexPath.row % 2 == 0 ? UIColor.orange :UIColor.brown
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("row: \(indexPath.row)")
    }

}

<file_sep>//
//  ViewController.swift
//  1_Test_TableView_CollectionView
//
//  Created by 1 on 2020/10/27.
//

import UIKit

class ViewController: UIViewController {
    
    //拳型
    let rps:[String] = ["paper", "rock", "scissors"]
    var winAndLose:[String] = []
    
    // 紀錄陣列
    var recordListRps: [String] = []
    
    // 猜拳圖片
    @IBOutlet weak var com: UIImageView!
    @IBOutlet weak var own: UIImageView!
    
    // 結果
    @IBOutlet weak var resultLabel: UILabel!
    
     // 開始猜拳
    @IBAction func mora(_ sender: UIButton) {
        // 隨機亂數 取出list
        let comIndex = Int(arc4random_uniform(UInt32(rps.count)))
        let ownIndex = Int(arc4random_uniform(UInt32(rps.count)))
        
        // 顯示圖片
        com.image = UIImage(named: rps[comIndex])
        own.image = UIImage(named: rps[ownIndex])
        
        checkWinner(ownIndex: ownIndex, comIndex: comIndex)

        print(winAndLose)
        print(recordListRps)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    //判斷
    func checkWinner(ownIndex :Int, comIndex: Int){
        // 勝負
        let checkWinner = (ownIndex, comIndex)
        switch checkWinner {
        case let (x, y) where x == y:
            recordListRps.append("我方：" + rps[ownIndex] + " 電腦：" + rps [comIndex] + " / 結果：平手")
            winAndLose.append("平手")
            return resultLabel.text! = "平手"
        case (0,1):
            recordListRps.append("我方：" + rps[ownIndex] + " / 電腦：" + rps [comIndex] + " / 結果：我方勝利")
            winAndLose.append("贏")
            return resultLabel.text! = "我方勝利"
        case (0,2):
            recordListRps.append("我方：" + rps[ownIndex] + " / 電腦：" + rps [comIndex] + " / 結果：電腦勝利")
            winAndLose.append("輸")
            return resultLabel.text! = "電腦勝利"
        case (1,0):
            recordListRps.append("我方：" + rps[ownIndex] + " / 電腦：" + rps [comIndex] + " / 結果：電腦勝利")
            winAndLose.append("輸")
            return resultLabel.text! = "電腦勝利"
        case (1,2):
            recordListRps.append("我方：" + rps[ownIndex] + " / 電腦：" + rps [comIndex] + " / 結果：我方勝利")
            winAndLose.append("贏")
            return resultLabel.text! = "我方勝利"
        case (2,0):
            recordListRps.append("我方：" + rps[ownIndex] + " / 電腦：" + rps [comIndex] + " / 結果：我方勝利")
            winAndLose.append("贏")
            return resultLabel.text! = "我方勝利"
        case (2,1):
            recordListRps.append("我方：" + rps[ownIndex] + " / 電腦：" + rps [comIndex] + " / 結果：電腦勝利")
            winAndLose.append("輸")
            return resultLabel.text! = "電腦勝利"
        case (_, _):
            return
        }
        
    }
    
    // 下一頁資訊
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "segue_rps_to_table" {
            let table_VC = segue.destination as! RecordTableViewController
            if recordListRps != [] {
                table_VC.recordListInTableView = recordListRps
            } else {
                table_VC.recordListInTableView = ["比賽尚未開始"]
            }
        }
        if segue.identifier == "segue_rps_to_collection"{
            let cellection_VC = segue.destination as! CollectionViewController
            cellection_VC.imageWinAndLose = winAndLose
        }
    }


}

<file_sep>//
//  CollectionViewCell.swift
//  1_Test_TableView_CollectionView
//
//  Created by 1 on 2020/10/28.
//

import UIKit

class CollectionViewCell: UICollectionViewCell {
    
    @IBOutlet weak var imageView: UIImageView!
    
    static var nib: UINib {
        return UINib(nibName: "CollectionViewCell", bundle: Bundle(for: self))
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

}
