꾸준히 안타치기

TableVIew / custom 본문

iOS/storyboard & code

TableVIew / custom

글자줍기 2021. 12. 21. 22:03
반응형

TableVIewController.swift

 

테이블에 동적으로 데이터 추가하기

현재 날짜를 가져와 행에 추가하는 코드 

import UIKit


class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
    
    @IBOutlet weak var tableView: UITableView!
    let cellIdentifier: String = "cell"
    
    let korean: [String] = ["가","나","다","라","마","바","가","나","다","라","마","바"]
    
    let english: [String] = ["A","B","C","D","E","F","A","B","C","D","E","F"]
    
    override func viewDidLoad(){
        super.viewDidLoad()
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
     }
    
    // 섹션을 몇개를 보여줄지?
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    // 섹션마다 무엇을 보여줄지?
    func tableView(_ tableView: UITableView, numberOfRowsInSection section:
      Int)->Int{
        
        switch section{
        case 0:
            return korean.count
        case 1:
            return english.count
        default:
            return 0
        }
    }
    
    // 줄에 해당하는 셀보여주기
    func tableView(_ tableView: UITableView, cellForRowAt indexPath:
                    IndexPath)-> UITableViewCell{
        
        let cell : UITableViewCell =
            tableView.dequeueReusableCell(withIdentifier: self.cellIdentifier,
            for: indexPath)
        
        let text: String = indexPath.section == 0 ? korean[indexPath.row] :
            english[indexPath.row]
        
        cell.textLabel?.text = text
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section:
                    Int) -> String?{
        return section == 0 ? "한글" : "영어"
    }
}

 

 

현재 날짜를 가져와 행에 추가하는 코드  + 추가한행만 업데이트 + 애니메이션 


import UIKit


class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
    
    @IBOutlet weak var tableView: UITableView!
    let cellIdentifier: String = "cell"
    
    let korean: [String] = ["가","나","다","라","마","바","가","나","다","라","마","바"]
    
    let english: [String] = ["A","B","C","D","E","F","A","B","C","D","E","F"]
    
    var dates: [Date] = []
    
    // 데이트피커 설정
    let dateFormatter: DateFormatter = {
        let formatter: DateFormatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeStyle = .medium
        return formatter
    }()
    
    // 버튼을 누를때 마다 테이블 뷰를 업데이트 한다.
    @IBAction func touchUPAddButton(_ sender: UIButton){
        dates.append(Date())
        
        // 전체데이터를 다가져오는 함수
//        self.tableView.reloadData()
        
        // 새로추가된 것만 가져오는 함수 + 애니메이션
        self.tableView.reloadSections(IndexSet(2...2), with:
                                        UITableView.RowAnimation.automatic)
    }
    
    
    override func viewDidLoad(){
        super.viewDidLoad()
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
     }
    
    // 섹션을 몇개를 보여줄지?
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    // 섹션마다 무엇을 보여줄지?
    func tableView(_ tableView: UITableView, numberOfRowsInSection section:
      Int)->Int{
        
        switch section{
        case 0:
            return korean.count
        case 1:
            return english.count
        case 2:
            return dates.count
        default:
            return 0
        }
    }
    
    // 줄에 해당하는 셀보여주기
    func tableView(_ tableView: UITableView, cellForRowAt indexPath:
                    IndexPath)-> UITableViewCell{
        
        let cell : UITableViewCell =
            tableView.dequeueReusableCell(withIdentifier: self.cellIdentifier,
            for: indexPath)
        
        if indexPath.section < 2 {
            let text: String = indexPath.section == 0 ? korean[indexPath.row] :
                english[indexPath.row]
            cell.textLabel?.text = text
        }else{
            cell.textLabel?.text = self.dateFormatter.string(from: self.dates[indexPath.row])
        }
        
       
      
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section:
                    Int) -> String?{
        if section < 2 {
            return section == 0 ? "한글" : "영어"
        }
        return nil
      
    }
}

 

https://www.boostcourse.org/mo326/lecture/16890?isDesc=false 

 

반응형
Comments