Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KentarouKanno/ef8d77f768fe0274992b0ca1f5ac0be6 to your computer and use it in GitHub Desktop.
Save KentarouKanno/ef8d77f768fe0274992b0ca1f5ac0be6 to your computer and use it in GitHub Desktop.

画面間の値渡し(TableView -> Cell)

import UIKit

class ViewController: UIViewController {
    
    
    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.dataSource = self
            tableView.delegate = self
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
    }
}

// MARK: - UITableViewDataSource

extension ViewController: UITableViewDataSource {
    
    // Row Count
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    // Generate Cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell {
            cell.textLabel?.text = "Section = \(String(indexPath.section)), row = \(String(indexPath.row))"
            cell.delegate = self
            return cell
        }
        return UITableViewCell()
    }
}

// MARK: - UITableViewDelegate

extension ViewController: UITableViewDelegate {
    
    // Select Cell
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)
        print("Touch Section = \(String(indexPath.section)), row = \(String(indexPath.row))")
    }
}

// MARK: - CustomCellDelegate

extension ViewController: CustomCellDelegate {
    func tappedButton() {
        print("Tapped!")
    }
}

// MARK: - CustomCell.swift

protocol CustomCellDelegate: class {
    func tappedButton()
}

class CustomCell: UITableViewCell {
    
    weak var delegate: CustomCellDelegate?
    
    @IBAction func tappedButton(_ sender: UIButton) {
        delegate?.tappedButton()
    }
}
  • セクション付き
import UIKit

class ViewController: UIViewController {
    
    var items: [String] = []
    
    enum SectionType {
        case header
        case contents
        
        static var count: Int {
            let type: [SectionType] = [.header, .contents]
            return type.count
        }
    }
    
    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.dataSource = self
            tableView.delegate = self
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        items.append("value1")
        items.append("value2")
        items.append("value3")
        
        tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
    }
}

// MARK: - UITableViewDataSource

extension ViewController: UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return SectionType.count
    }
    
    // Row Count
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    // Generate Cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell {
            let text = items[indexPath.row]
            cell.textLabel?.text = "Section = \(String(indexPath.section)), row = \(String(indexPath.row)), text = \(text)"
            cell.delegate = self
            return cell
        }
        return UITableViewCell()
    }
}

// MARK: - UITableViewDelegate

extension ViewController: UITableViewDelegate {
    
    // Select Cell
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)
        print("Touch Section = \(String(indexPath.section)), row = \(String(indexPath.row))")
    }
}

// MARK: - CustomCellDelegate

extension ViewController: CustomCellDelegate {
    func tappedButton() {
        print("Tapped!")
    }
}



protocol CustomCellDelegate: class {
    func tappedButton()
}

class CustomCell: UITableViewCell {
    
    weak var delegate: CustomCellDelegate?
    
    @IBAction func tappedButton(_ sender: UIButton) {
        delegate?.tappedButton()
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment