#dynamic tableview


tableviewをviewコントローラに入れればいいです.
アンカーポイントを設定する場合はsafe area->superviewを使用して画面の末尾が表示されます
セルを入れるときは直接入れるか、空のファイル(cmd+n->empty)を作成してセルを作成できます.
コントロールを押してドラッグすると、2つのビューの間にheight、widthなどを位置合わせできます.
イメージをassetに読み込み、イメージビューに追加できます.

->image viewとstack viewの使用(分布->等しい)

->stackviewを使用してハート、賛、および共有シェイプを挿入します(Containtメニューではなく、stackviewメニューでスペースで区切ることができます).ラベルバーはor equalおよびlineより大きく0でスケールされます.
コードの作成
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        <#code#>
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        <#code#>
    }
    
} //tableviewdelegate는 tableview에 대한 설정을 하는 것이고, datasource는 source를 집어넣는 것
この方法と.
class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


    
    
}


extension ViewController: UITableViewDelegate {
    

}

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        <#code#>
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        <#code#>
    }
    
    
}
どちらでもいいです

->右側の画面を追加し、cmd+shift+Oを使用してstoryboardに設定します.左はcellが追加された内容です.

->右側のtableviewをクリックし、ドラッグ&ドロップして@Iboutletを作成します(右側のviewcontrollerのクラスは、ドラッグ&ドロップで作成するには、左側でクラスとして指定されたViewControllerに位置合わせする必要があります)

->cmd+nを使用してswiftファイルを作成します.さっき作成したセルに関連付けられた他のクラスが必要です.

->コードとuiを関連付ける必要があるので、作成中のcustom classに関連付ける必要があります.

->まずはイメージのアウトレット

画像を丸くするコード


MyTableViewCell swiftファイルのAwakeFromNib()で作成(xibファイルはNibと呼ばれます)
    override func awakeFromNib() {
        super.awakeFromNib()
        
        print("MyTableViewCell - awakeFromNib() called")
        
        userProfileImg.layer.cornerRadius = userProfileImg.frame.height / 2
    }
}

ViewControlコード


クラスの上書き
override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        //cell 리소스 파일 가져오기
        let myTableViewCellNib = UINib(nibName: String(describing: MyTableViewCell.self), bundle: nil)
        
        //cell에 리소스 등록
        self.myTableView.register(myTableViewCellNib, forCellReuseIdentifier: "myTableViewCEll")
        //height정해주기(윗줄은 알아서 정해주는거고, 아랫줄은 정해주는 것)
        self.myTableView.rowHeight = UITableView.automaticDimension
        self.myTableView.estimatedRowHeight = 120
        
        //아주 중요******(UITableViewDelegate과 UITableViewDataSource를 연결해줘야함)
        self.myTableView.delegate = self
        self.myTableView.dataSource = self
        //content가 몇갠지
        print("contentArray.count: \(contentArray.count)")
    }
heightを決定すると、1つのセルのセルが自動的にビューを計算して表示されます.
次の拡張コード
extension ViewController: UITableViewDelegate {
    

}

extension ViewController: UITableViewDataSource {
    
    //테이블 뷰 cell의 갯수
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.contentArray.count
    }
    
    //각 cell에 대한 설정, 드래그할때마다 cellForRowAt이 호출됨 -> 데이터와 뷰를 연결시켜주는 역할
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = myTableView.dequeueReusableCell(withIdentifier: "myTableViewCEll", for: indexPath) as! MyTableViewCell //Identifier는 위에 forCellReuseIdentifier과 똑같이 써줘야함, for는 드래그할때마다 호출되면서 인덱스를 넘겨주는데 그것을 indexpath, 이것의 자료형을 강제로 하겠다 -> MyTableViewCell
        
        cell.userContentLabel.text = contentArray[indexPath.row]
        
        return cell
    }
    
    
}

//tableviewdelegate는 tableview에 대한 설정을 하는 것이고, datasource는 source를 집어넣는 것
誤り
[WindowScene] Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
->このようなメッセージが表示された場合は、「ビューア」に移動し、「is Initial View Control」で最初の「コントローラの表示」を設定します.
結果

完全なコード


ViewController

import UIKit

class ViewController: UIViewController {
    
        
    @IBOutlet weak var myTableView: UITableView!
    
    
    
    let contentArray = [
    "HaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHa",
        
    "ABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABC",
        
    "CDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDFCDF",
        
    "EFGEFGEFGEFGEFGEFGEFGEFGEFGEFGEFGEFGEFGEFGEFGEFGEFGEFG",
        
    "HIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJHIJ",
        
    "KLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLMKLM",
        
    "NOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOPNOP"
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        //cell 리소스 파일 가져오기
        let myTableViewCellNib = UINib(nibName: String(describing: MyTableViewCell.self), bundle: nil)
        
        //cell에 리소스 등록
        self.myTableView.register(myTableViewCellNib, forCellReuseIdentifier: "myTableViewCEll")
        //height정해주기(윗줄은 알아서 정해주는거고, 아랫줄은 정해주는 것)
        self.myTableView.rowHeight = UITableView.automaticDimension
        self.myTableView.estimatedRowHeight = 120
        
        //아주 중요******(UITableViewDelegate과 UITableViewDataSource를 연결해줘야함)
        self.myTableView.delegate = self
        self.myTableView.dataSource = self
        //content가 몇갠지
        print("contentArray.count: \(contentArray.count)")
    }

    
}


extension ViewController: UITableViewDelegate {
    

}

extension ViewController: UITableViewDataSource {
    
    //테이블 뷰 cell의 갯수
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.contentArray.count
    }
    
    //각 cell에 대한 설정, 드래그할때마다 cellForRowAt이 호출됨 -> 데이터와 뷰를 연결시켜주는 역할
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = myTableView.dequeueReusableCell(withIdentifier: "myTableViewCEll", for: indexPath) as! MyTableViewCell //Identifier는 위에 forCellReuseIdentifier과 똑같이 써줘야함, for는 드래그할때마다 호출되면서 인덱스를 넘겨주는데 그것을 indexpath, 이것의 자료형을 강제로 하겠다 -> MyTableViewCell
        
        cell.userContentLabel.text = contentArray[indexPath.row]
        
        return cell
    }
    
    
}

//tableviewdelegate는 tableview에 대한 설정을 하는 것이고, datasource는 source를 집어넣는 것

MyTableViewCell

import Foundation
import UIKit

class MyTableViewCell: UITableViewCell { //상속은 UITableViewCell이 받음
    
    @IBOutlet weak var userProfileImg: UIImageView!
    
    @IBOutlet weak var userContentLabel: UILabel!
    
    
    //cell이 렌더링(뷰를 그릴때) 될때
    override func awakeFromNib() {
        super.awakeFromNib()
        
        print("MyTableViewCell - awakeFromNib() called")
        
        userProfileImg.layer.cornerRadius = userProfileImg.frame.height / 2
    }
}

難しいのでもう一度やります


リファレンス


鄭代理のYoutube