Storyboard | Tableview (Alert text input)

일어나 개발해야지·2023년 8월 8일

iOS 글쓰기모임

목록 보기
13/13

순서

1)Storyboard로 Tableview를 그리는것을 시도
2)delegate의 개념에 대해서도 정리
3)Alert으로 text를 입력하는 코드를 적용

import UIKit

class TodoListPage: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // 뷰 컨트롤러 초기화 작업 수행
        
        tableView.delegate = self
        tableView.dataSource = self
    }
}

extension TodoListPage: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // 실제 데이터의 행 수 반환
        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)

        cell.textLabel?.text = "\(indexPath.row + 1)"

        return cell
    }
    //New
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // 셀이 선택되었을 때 호출되는 메서드
        showTextAlert(for: indexPath)
    }

    // 사용자 정의 함수: 텍스트 입력을 받는 알림창을 표시하는 메서드
    func showTextAlert(for indexPath: IndexPath) {
        let alertController = UIAlertController(title: "텍스트 입력", message: "원하는 텍스트를 입력하세요.", preferredStyle: .alert)

        alertController.addTextField { textField in
            textField.placeholder = "텍스트 입력"
        }

        let cancelAction = UIAlertAction(title: "취소", style: .cancel, handler: nil)
        let saveAction = UIAlertAction(title: "저장", style: .default) { [weak self, weak alertController] _ in
            guard let textField = alertController?.textFields?.first,
                  let newText = textField.text else {
                return
            }
            // 입력받은 텍스트를 셀에 적용
            self?.updateText(newText, for: indexPath)
        }

        alertController.addAction(cancelAction)
        alertController.addAction(saveAction)

        present(alertController, animated: true, completion: nil)
    }

    // 사용자 정의 함수: 텍스트를 셀에 업데이트하는 메서드
    func updateText(_ newText: String, for indexPath: IndexPath) {
        guard let cell = tableView.cellForRow(at: indexPath) else {
            return
        }
        cell.textLabel?.text = newText
    }
}

동작하지 않았던 코드 (분석예정)

import UIKit

// New: Define the CustomTableViewCell class
class CustomTableViewCell: UITableViewCell {
    @IBOutlet weak var textField: UITextField!
}

class TodoListPage: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do the view controller initialization work.
        
        tableView.delegate = self
        tableView.dataSource = self
    }
}

extension TodoListPage: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the actual number of rows of data.
        return 2
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath) as! CustomTableViewCell

        // Fix: Set UITextField inside the cell
        cell.textField.delegate = self
        cell.textField.tag = indexPath.row
        cell.textField.placeholder = "Type here"

        return cell
    }
}

extension TodoListPage: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // Handles the action when the return/enter key is pressed.
        textField.resignFirstResponder() // Hide the keyboard
        return true
    }
}

0개의 댓글