SeSAC_iOS_Day 15 | TIL


👩‍💻 カリキュラムと追加学習


📂 RawString

  • 文字列でエスケープ文字または境界文字を使用する場合は、文字列の両端に#を付けることができます.
  • var notice = #"온라인 라이브 수업은 "Zoom"으로 진행합니다."#
    「Zoom」で行われている、勘定科目のオンライン・ライブ・レッスン.
    var notice = #"온라인 라이브 수업은 \Zoom\으로 진행합니다."#
    オンライン生放送レッスンはZoomで行います.
  • ただし、エスケープ文字の機能を使用する場合は、\と文字の間に#を追加するだけです.
  • var notice = #"온라인 라이브 수업은 \#n\Zoom\으로 진행합니다."#
    勘定科目のオンライン・ライブ・レッスン
    \Zoom\.
  • 文字列補間を使用するには、同じ#を追加する必要があります.
  • var onlineService = "WhaleOn"
    var notice = #"온라인 라이브 수업은 \#(onlineService)으로 진행합니다."#
    勘定科目のオンライン・ライブ・レッスンはWhaleonで行います.

    📂 WKWebView & Search Bar

  • Webビュー、WebKit Viewを使用できます.
  • SearchBarも接続しなければなりませんdelegate!
  • 検索後にリターンキーを押した場合に実行する方法:searchBarSearchButtonClickedUI SearchBarDelegteプロトコルで実装される方法の1つ|918;8|
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    	// guard 문으로 searchBar에 입력된 문자열을 URL로 변경해주고 request 후 그 결과를 webView에 띄워준다.
            guard let url = URL(string: searchBar.text ?? "") else {
                print("ERROR")
                return
            }
            
            let request = URLRequest(url: url)
            webView.load(request)
        }

    📂 App Transport Security Settings


  • アプリケーションがサーバに送信するデータのセキュリティ設定

  • HTTPプロトコルを使用した外部サーバとの通信を設定

  • HTTPS(セキュリティ):パケットの暗号化と転送によりセキュリティを確保する

  • すべてのドメインに対してHTTP通信を許可する:安全性の面でこの方式を採用することを提案しない

  • 📂 UICollectionView


    1.CollectionView Outletへの接続

    @IBOutlet var tagCollectionView: UICollectionView!

    2. CollectionView Protocol: UICollectionViewDelegate, UICollectionViewDataSource

  • numberOfItemsInSection
    -コード
  • 合計何個必要か教えてください
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 10
        }
  • 1つのビューコントローラに複数のCollectionViewが含まれている場合は、次のようにCollectionViewを区別できます.
  •     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            // 이 코드 대신 tag 설정해두고 사용해도됨
            return collectionView == tagCollectionView ? 10 : mainArray.count
        }
  • CellForItemAt:tableViewのrowitemに変更した以外は、コード構成はほぼ同じです.
  • func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TagCollectionViewCell.identifier, for: indexPath) as? TagCollectionViewCell else {
                    return UICollectionViewCell()
                }
             return cell
        }

    3.Delegte、データソース接続

    tagCollectionView.delegate = self
    tagCollectionView.dataSource = self

    4.XIB登録

    let secondNibName = UINib(nibName: TagCollectionViewCell.identifier, bundle: nil)
    tagCollectionView.register(secondNibName, forCellWithReuseIdentifier: TagCollectionViewCell.identifier)

    📂 UICollectionViewFlowLayout


    上下左右の余白(垂直部分)+セルとセルの間隔+セル上下の余白

    FlowLayout instrumsの作成

    let layout = UICollectionViewFlowLayout()

    itemSizeの設定

    // 각 셀과 셀 사이의 간격 설정
    let spacing: CGFloat = 20
    // 3개의 열로 세팅하기 위해서 전체 폭에서 4개의 간격만큼 값을 빼줌
    let width = UIScreen.main.bounds.width - (spacing * 4)
    // 전체 폭을 3으로 나눈 값만큼 폭을 설정해주고 이에 비례하여 높이도 설정해줌
    layout.itemSize = CGSize(width: width / 3, height: width / 3 * 1.2)

    minimumLineSpacing


    行と行の間の最小間隔の設定
    layout.minimumLineSpacing = spacing

    minimumInteritemSpacing


    列と列の最小間隔の設定
    layout.minimumInteritemSpacing = spacing

    scrollDirection


    スクロール方向を横または縦に設定できます
    layout.scrollDirection = .vertical

    sectionInset


    コンテンツ領域内のマージン値を設定できます(この部分はスクロール可能領域とも考えられます).
    layout.sectionInset = UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing)

    応用Layout

    mainCollectionView.collectionViewLayout = layout

    Tag - AddTarget & ReloadRows


  • 現在、ViewControllerには100個の値falseMainArray配列があります.

  • Heartボタンをクリックするたびに埋まったHeartと空のHeartが出てきます

  • まず配列を発表する.
  • var mainArray = Array(repeating: false, count: 100)
  • cellForItemAtメソッドでは、indexPath値を使用して各セルのボタンにタグを追加できます.これにより、配列内の各インデックスの値とボタンを一致させることができます.
  • let item = mainArray[indexPath.item]
    let image = item == true ? UIImage(systemName: "heart.fill") : UIImage(systemName: "heart")
    cell.heartButton.setImage(image, for: .normal)
    cell.mainImageView.backgroundColor = .red
    cell.heartButton.tag = indexPath.item
  • ここでは、クリックするたびに画像を変更できる方法を追加する必要があります.まずボタンにaddTargetを追加します.
  • cell.heartButton.addTarget(self, action: #selector(heartButtonTapped(selectButton:)), for: .touchUpInside)
  • selectorは、Objective-Cを識別するために@objcを前に付けなければならない.
    このボタンをクリックするたびに、このボタンが持つタグ値を使用して、配列内のインデックスの値を現在の逆の値に更新します.
  • 次に、ビューに適用するために更新された値を再ロードする.この場合はreloadData()を使用できますが、ある場所の値を更新するだけであればreloadItems(at:)を使用できます.tableViewは、同じ機能のメソッドをreloadRowsと定義します.
  • @objc func heartButtonTapped(selectButton: UIButton) {
            mainArray[selectButton.tag] = !mainArray[selectButton.tag]
            mainCollectionView.reloadItems(at: [IndexPath(item: selectButton.tag, section: 0)])
        }

    👩‍💻 Mission


    Core Location


    Meet the Location Button(WWDC21)
    What's new in location(WWDC20)
    What's New in Core Location(WWDC15)