천원의 개발

IOS TableView 커스텀 본문

iOS&Swift🍎

IOS TableView 커스텀

천 원 2022. 3. 31. 01:38

먼저 XIB를 체크하고 새로운 파일을 생성한다

그후 만들어진 XIB파일을 커스텀한다 커스텀한 table에 identifier설정해주고 나는 CustomTable이라고 설정했다

자 이제 TableViewcontroller로 가서

class TableViewController: UITableViewController {

    @IBOutlet var tableVIew: UITableView!
    var contact: [Contact] = [
        Contact(relationship: "엄마", number: "01022224444"),
        Contact(relationship: "아빠", number: "01044447777")
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "CustomTable")
    }
    
    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTable", for: indexPath) as! TableViewCell //
        
        cell.reationLabel.text = contact[indexPath.row].relationship
        cell.phoneNumberLabel.text = String(contact[indexPath.row].number)
        
        return cell
        
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return contact.count
    }
}

요로코롬 추가 해주면 완성!