iOS

[iOS] 2025.03.04 팀프로젝트(2)

ioskkt 2025. 3. 4. 21:27

메인페이지와 하위페이지를 모달로 연결하고 싶은데 여전히 오류 발생함.

우선 메인페이지만 만든 코드만 기록해보겠음.

import UIKit

class ViewController: UIViewController {
               
    @IBOutlet weak var firstTableView: UITableView!
    @IBOutlet weak var secondTableView: UITableView!
    
    let teamCharacteristic: [String] = ["우리팀의 특징", "궁극적인 목표", "우리팀의 약속"]
    let teammate: [String] = ["🙋🏻‍♀️박혜민", "🙆🏻‍♂️김기태", "🙋🏻‍♂️김형윤", "💁🏻‍♂️변준영"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        firstTableView.backgroundColor = .systemRed
        firstTableView.delegate = self
        firstTableView.dataSource = self
        
        secondTableView.backgroundColor = .systemBlue
        secondTableView.delegate = self
        secondTableView.dataSource = self
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    
    // 첫 번째 테이블 뷰의 셀 수
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == firstTableView {
            return teamCharacteristic.count
        } else if tableView == secondTableView {
            return teammate.count
        }
        return 0
    }
    
    // 첫 번째 테이블 뷰의 셀 내용
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == firstTableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "FirstCell", for: indexPath)
            cell.textLabel?.text = teamCharacteristic[indexPath.row]
            return cell
        } else if tableView == secondTableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath)
            cell.textLabel?.text = teammate[indexPath.row]
            return cell
        }
        return UITableViewCell()
    }
}

 

 

    let teamCharacteristic: [String] = ["우리팀의 특징", "궁극적인 목표", "우리팀의 약속"]
    let teammate: [String] = ["🙋🏻‍♀️박혜민", "🙆🏻‍♂️김기태", "🙋🏻‍♂️김형윤", "💁🏻‍♂️변준영"]
  • Array를 통해서 팀특성, 팀원 이름을 구성함.

 

        firstTableView.backgroundColor = .systemRed
        firstTableView.delegate = self
        firstTableView.dataSource = self
        
        secondTableView.backgroundColor = .systemBlue
        secondTableView.delegate = self
        secondTableView.dataSource = self

//백그라운드 컬러는 두 개의 테이블 뷰가 연결되어있는지 확인하는 용도였고, 삭제할 것.

  • .delegate는 tableView에서 발생하는 이벤트를 ViewController에서 처리하도록 지정하는 것을 의미.
  • .dataSource는  ViewController가 데이터의 원천이 되도록 지정하는 것을 의미.

 

extension ViewController: UITableViewDelegate, UITableViewDataSource {
  • ViewController 클래스를 확장하여 보기 쉽게 정리함.
  • ViewController가 UITableViewDataSource라는 프로토콜을 채택하여 구현한다는 의미로 해석함.

 

 

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == firstTableView {
            return teamCharacteristic.count
        } else if tableView == secondTableView {
            return teammate.count
        }
        return 0
        
         func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == firstTableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "FirstCell", for: indexPath)
            cell.textLabel?.text = teamCharacteristic[indexPath.row]
            return cell
        } else if tableView == secondTableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath)
            cell.textLabel?.text = teammate[indexPath.row]
            return cell
        }
        return UITableViewCell()
    }
  • 프로토콜을 구현하기 위해서는 numberOfRowsInSection, cellForRowAt이 필요함.
  • numberOfRowsInSection 메서드는 테이블 뷰가 몇 개의 셀을 만들어야 할지 알 수 있도록 지정하는 역할을 함. 미 구현시, 셀의 개수를 알 수 없기 때문에 정상적으로 표현되지 않음.
  • cellForRowAt 메서드는 각 셀에 데이터를 어떻게 표현할지 지정하는 역할을 함. 미 구현 시, 오류 발생.

 

  • if, else if 조건문 사용 : 스토리보드에 두 개의 테이블 뷰를 추가했기 때문에, 각각 나타내기 위해서 조건문 이용함.

dequeueReusableCell이란?

사용자가 스크롤하여 화면에서 사라지는 셀을 재사용할 수 있도록 큐에 저장하고, 필요한 셀을 재사용 가능할 때 다시 꺼내서 사용하는 방식.


  • indexPath위치에 있는, Identifier(식별자)가 FirstCell인 셀을 재사용하거나 생성함.
  • return을 통해 해당 셀을 반환하여 해당 테이블 뷰에서 사용되어 화면에 표시됨.(반환 : 어떤 값을 호출한 곳에 되돌려 준다고 이해하면 편함. 메서드나 함수가 작업을 마친 후 결과를 보내주는 것임.)

 

 

 

* 하위페이지 모달로 연결중인데, 원하는 곳을 클릭했을 때 원하는 페이지가 나오지 않아서 계속 수정중.

* 모르는 부분 Chat GPT 이용을 많이 했는데, 해당 공부 방식이 실질적으로 도움이 되는 건지에 대한 고민이 계속해서 있었음. 권영호 튜터의 조언에 따라 시간이 걸리더라도 구글링을 통해 나와 유사한 프로젝트에서 힌트를 얻고 해결하면서 기술을 체득하는 것이 좋다고 판단.