> For the complete documentation index, see [llms.txt](https://pakornpat.gitbook.io/ios-app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pakornpat.gitbook.io/ios-app/v2/adding-new-delegate.md).

# Adding new delegate

## ทำให้ปุ่ม Checkmark ใช้งานได้

ทีนี้เราก็เหลืออย่างเดียวละ คือทำให้ปุ่ม checkmark สามารถ toggle ค่า isDone ได้

เนื่องจาก TodoItemTableViewCell เป็น View เราจะไม่แก้ไข TodoItem เอง เราจึงสร้าง delegate ขึ้นมาใหม่เพื่อให้ Controller เราที่เป็นคนใช้ View นี้จัดการกับ Model เอง

เพิ่ม Protocol **TodoItemTableViewCellDelegate** กำหนดฟังก์ชั่น 1 อัน เพื่อเรียกว่าปุ่ม Checkbox โดนกดแล้วนะ

สร้างตัวแปรเก็บ delegate แก้ให้ **configure(item:)** รับ delegate ด้วย พร้อมกับสร้าง Action สำหรับรับ event ตอนปุ่มโดนกด โดยจะเรียกกลับไปที่ delegate

{% code title="TodoItemTableViewCell.swift" %}

```swift
import UIKit

protocol TodoItemTableViewCellDelegate:class {
    func todoItemTableViewCellDidTapCheckboxButton(cell: TodoItemTableViewCell)
}

class TodoItemTableViewCell: UITableViewCell {

    weak var delegate: TodoItemTableViewCellDelegate?

    @IBOutlet weak var checkboxButton: UIButton?
    @IBOutlet weak var titleLabel: UILabel?

    func configure(item: TodoItem, delegate: TodoItemTableViewCellDelegate?) {
        titleLabel?.text = item.title
        checkboxButton?.setImage(UIImage(named: item.isDone ? "check": "uncheck"), for: .normal)
        self.delegate = delegate
    }
    
    @IBAction func checkboxButtonDidTap() {
        delegate?.todoItemTableViewCellDidTapCheckboxButton(cell: self)
    }

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

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}
```

{% endcode %}

จากนั้นผูกปุ่มกับ  Action ที่เราสร้างโดยให้เรียกเมื่อ Touch up inside นั่นคือแตะแล้วปล่อยบนปุ่ม

![](/files/-LfNegEFtRvYb0-9F4Wi)

จากนั้นที่ ViewController ของเราให้ประกาศตัวเป็น **TodoItemTableViewCellDelegate** พร้อมส่งตัวเองเป็น delegate มาตอน configure cell ใน **tableView(\_:cellForRowAt:)**

```swift
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "todoItemCell", for: indexPath) as! TodoItemTableViewCell
    let item = todo.item(at: indexPath.row)
    cell.configure(item: item, delegate: self)
    return cell
}
```

จากนั้นเขียนฟังก์ชั่น delegate แบบนี้ เมื่อรับ event มาเราจะหาก่อนว่า cell ส่ง event มา เป็น cell ไหน todoItem อะไรที่เราต้องแก้ isDone แล้วสั่ง toggle พร้อมกับ reload ที่ cell นั้น

```swift
func todoItemTableViewCellDidTapCheckboxButton(cell: TodoItemTableViewCell) {
    if let indexPath = tableView?.indexPath(for: cell) {
        todo.item(at: indexPath.row).isDone.toggle()
        tableView?.reloadRows(at: [indexPath], with: .automatic)
    }
}
```

เป็นอันจบ ลองรันได้

![](/files/-LfNffY_D0qRob6AeeVM)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pakornpat.gitbook.io/ios-app/v2/adding-new-delegate.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
