Cell Delegate 실습

CustomCell설계 및 delegate 패턴 실습

CustomCell의 스위치 value가 바뀔 때마다 ViewController에게 value 값을 전달해주기 위해, delegate 패턴을 만들어본다.

01. UITableViewCell을 상속받은 CustomCell설계

CustomCellDelegate 프로토콜

protocol CustomCellDelegate {
    func customCellSwitched(_ cell:CustomCell, didChangedSwitch value:Bool)
}

CustomCell에서 delegate 속성 설정

import UIKit
class CustomCell: UITableViewCell {
    
    @IBOutlet var switcher: UISwitch!
    
    var cellDelegate:CustomCellDelegate?
    var indexPath:IndexPath?
    var switchData:Bool = true {
        willSet {
            self.switcher.isOn = newValue
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    @IBAction func switchBtnHandler(_ sender: UISwitch) {
        cellDelegate?.customCellSwitched(self, didChangedSwitch: sender.isOn)
    }
}

02. ViewController에서 CustomCellDelegate 프로토콜의 메소드 구현

import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomCellDelegate {
    
    var onOffList:[Bool] = [true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true] 
    // 데이터는 테스트용으로 스위치의 Bool값 설정

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return onOffList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
        cell.cellDelegate = self
        cell.switchData = onOffList[indexPath.row]
        cell.textLabel?.text = "\(indexPath.row)" + "Welcome to Delegate World!"
        cell.indexPath = indexPath
        return cell
    }
    
    
    func customCellSwitched(_ cell: CustomCell, didChangedSwitch value: Bool)
    {
        onOffList[cell.indexPath!.row] = value
    }
    
}