티스토리 뷰

Mobile/ios

[ios] ios 11: Table View Controller

춘햄 2022. 12. 20. 17:41

데이터를 목록 형태로 보여 주기 위한 가장 좋은 방법은 테이블 뷰 컨트롤러를 이용하는 것이다.

 

테이블 뷰 컨트롤러는 사용자에게 목록 형태의 정보를 제공해 줄 뿐만 아니라 목록의 특정 항목을 선택하여 세부 사항을 표시할 때 유용하다.

 

이번 포스팅에서는 테이블 뷰를 사용하여 데이터를 목록 상에 표시하고 추가, 삭제 기능을 넣어 간단한 리스트 앱을 만들어 보려고 한다.

 

바로 확인해보자.


우선, 새 프로젝트를 하나 생성하고 Table View Controller 를 사용할 것이기 때문에 기존에 있던 ViewContoller.swift 파일과 스토리보드의 ViewController 를 선택하여 삭제한다.

 

이후에 Table View Contoller를 라이브러리 목록에서 찾아 추가한다.

 

이제 테이블에 들어갈 새로운 리스트를 추가하고, 편집하려면 두 개의 뷰 컨트롤러가 필요하다. 두 개의 View Controller를 전환할 수 있게 해주는 Navigation Controller를 아래와 같이 Table View Controller의 Prototype Cells 를 클릭하고 추가한다.

추가된 Navigation Controller가 앱이 실행될 때 처음으로 실행될 View가 되어야 하기 때문에 아래와 같이 설정해준다.

 

다음으로 데이터를 새로 추가할 Add View Controller 와 해당 데이터의 세부 정보를 확인할 Detail View Controller 를 추가한다.

 

Table View Controller에서 Add 버튼을 하나 추가한 뒤, Add 버튼은 Add View로, Cell은 Detail View로 이동하도록 Segue Way를 이어준다.

 

또한 DetailView로 가능 Segue Way는 sgDetail 이라는 Identifier를 추가해준다.

 

이제 각 View의 타이틀을 입력해주고, 아래와 같이 스토리보드를 구성해준다.

 

마지막으로 Table View의 Cell이 myCell이라는 Identifier를 가지도록 지정해주고,

 

UITableViewController 를 상속한 TableViewController.swift 파일과 UIViewController를 상속한 AddViewController, DetailViewController를 추가하여 각 스토리보드 객체와 연결해주면 기본적인 개발 준비는 모두 끝이 난다.


 코드를 작성하기 전에, TableViewController를 한번 살펴보면, TableViewContoller.swfit 는 UIViewController를 상속받은 일반 View Controller 소스와는 달리 상당히 많은 오버라이드 메서드가 선언되어 있고, 주석 처리되어 있는 것을 볼 수 있는데 이는 테이블 뷰에 목록을 추가하거나 삭제, 화면 이동과 같은 동작들을 수행할 수 있도록 주석 처리를 해제하고 개발자가 재정의하여 사용하도록 xCode가 자동으로 생성한 코드이다.

 

사용할 필요가 없다면 삭제해도 무방하지만, 보통의 경우 Table View를 다룰 때 반드시 필요한 메서드들이니 일단은 놔두도록 하자.


1. 목록 구성하기

 

우선, TableViewController 에 UITableView 객체를 outlet 변수로 연결하고, 사용할 사진과 리스트 제목을 가지는 리스트 객체를 선언해주자.

 

◎TableViewController.swift

import UIKit

var items = ["책 구매", "철수와 약속", "스터디 준비하기"]
var itemImageFile = ["cart.png", "clock.png", "pencil.png"]

class TableViewController: UITableViewController {

    @IBOutlet var tbListView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
}

 

이제 기본적으로 제공되는 오버라이드 메서드인 numberOfSections(in tableView: UITableView) 메서드와 

tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) 를 재정의하여 UITableView객체가 가질 섹션의 수와 한 섹션이 가질 아이템의 개수를 지정해준다.

 

◎TableViewController.swift

import UIKit

var items = ["책 구매", "철수와 약속", "스터디 준비하기"]
var itemImageFile = ["cart.png", "clock.png", "pencil.png"]

class TableViewController: UITableViewController {

    @IBOutlet var tbListView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // MARK: - Table view data source
    // 보통 테이블 안에 섹션이 한개이기 때문에 numberOfSections 는 1을 return 하도록 작성
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    // 섹션당 열의 개수는 items의 개수로
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return items.count
    }
}

 

다음으로 주석 처리되어 있던 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 메서드를 주석 해제하고 아래와 같이 재정의하여 myCell이라는 Cell객체에 각 데이터를 삽입하고 Cell 객체를 반환하도록 해주면 목록을 구성하는 과정은 모두 끝이 난다.

 

◎TableViewController.swift

import UIKit

var items = ["책 구매", "철수와 약속", "스터디 준비하기"]
var itemImageFile = ["cart.png", "clock.png", "pencil.png"]

class TableViewController: UITableViewController {

    @IBOutlet var tbListView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    // MARK: - Table view data source
    // 보통 테이블 안에 섹션이 한개이기 때문에 numberOfSections 는 1을 return 하도록 작성
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    // 섹션당 열의 개수는 items의 개수로
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return items.count
    }

    // 주석되어 있던 함수: Table Cell을 채우기 위한 함수
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // table view 내부 Cell의 identifier로 수정
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
        
        // 셀의 텍스트 레이블에 앞에서 선언한 items을 대입한다.
        cell.textLabel?.text = items[(indexPath as NSIndexPath).row]
        cell.imageView?.image = UIImage(named: itemImageFile[(indexPath as NSIndexPath).row])

        // Configure the cell...

        return cell
    }

}

 


2. 목록 삭제하기

 

목록 구성이 끝이 나면, 마찬가지로 주석 처리되어 있던

tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)

메서드를 주석 해제하고 아래와 같이 재정의하여 왼쪽으로 Cell을 밀어 삭제 버튼을 나타내고, 버튼을 누르면 삭제 목록을 삭제하는 동작을 구현한다.

 

◎TableViewController.swift

import UIKit

var items = ["책 구매", "철수와 약속", "스터디 준비하기"]
var itemImageFile = ["cart.png", "clock.png", "pencil.png"]

class TableViewController: UITableViewController {

    @IBOutlet var tbListView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // MARK: - Table view data source
    // 보통 테이블 안에 섹션이 한개이기 때문에 numberOfSections 는 1을 return 하도록 작성
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    // 섹션당 열의 개수는 items의 개수로
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return items.count
    }

    // 주석되어 있던 함수: Table Cell을 채우기 위한 함수
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // table view 내부 Cell의 identifier로 수정
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
        
        // 셀의 텍스트 레이블에 앞에서 선언한 items을 대입한다.
        cell.textLabel?.text = items[(indexPath as NSIndexPath).row]
        cell.imageView?.image = UIImage(named: itemImageFile[(indexPath as NSIndexPath).row])

        // Configure the cell...

        return cell
    }
    
    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    
    // Override to support editing the table view.
    // 주석되어 있던 함수: Table Cell을 삭제하기 위한 함수
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            items.remove(at: (indexPath as NSIndexPath).row)
            itemImageFile.remove(at: (indexPath as NSIndexPath).row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    
    // Delete 버튼의 문구를 수정하는 Override 함수
    override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?
    {
        return "삭제"
    }
    

}

3. 바 버튼으로 목록 삭제하기

 

코드에 적어놓지는 않았는데, 주석 처리된 오버라이드 메서드 말고도 아래와 같이 viewDidLoad 메서드 내부도 주석 처리된 바 버튼의 메모리 할당 함수가 있다.

override func viewDidLoad() {
        super.viewDidLoad()
        
        //self.navigationItem.rightBarButtonItem = self.editButtonItem
}

이를 주석 해제하고 그대로 사용하면 오른쪽에 바 버튼을 사용하지만, 오른쪽 바에는 Add 버튼이 존재하니 아래와 같이 left로 수정한다.

 

◎TableViewController.swift

import UIKit

var items = ["책 구매", "철수와 약속", "스터디 준비하기"]
var itemImageFile = ["cart.png", "clock.png", "pencil.png"]

class TableViewController: UITableViewController {

    @IBOutlet var tbListView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.leftBarButtonItem = self.editButtonItem
    }

    // MARK: - Table view data source
    // 보통 테이블 안에 섹션이 한개이기 때문에 numberOfSections 는 1을 return 하도록 작성
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    // 섹션당 열의 개수는 items의 개수로
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return items.count
    }

    // 주석되어 있던 함수: Table Cell을 채우기 위한 함수
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // table view 내부 Cell의 identifier로 수정
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
        
        // 셀의 텍스트 레이블에 앞에서 선언한 items을 대입한다.
        cell.textLabel?.text = items[(indexPath as NSIndexPath).row]
        cell.imageView?.image = UIImage(named: itemImageFile[(indexPath as NSIndexPath).row])

        // Configure the cell...

        return cell
    }
    
    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    
    // Override to support editing the table view.
    // 주석되어 있던 함수: Table Cell을 삭제하기 위한 함수
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            items.remove(at: (indexPath as NSIndexPath).row)
            itemImageFile.remove(at: (indexPath as NSIndexPath).row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    
    // Delete 버튼의 문구를 수정하는 Override 함수
    override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?
    {
        return "삭제"
    }
    
}

위와 같이 코드를 구성하여 왼쪽 바 버튼을 추가하고, 바 버튼을 터치하여 삭제 동작을 수행할 수 있게 되었다.

 


4. 목록 순서 바꾸기

 

마찬가지로 override된 tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) 메서드를 아래와 같이 재정의하여 목록 Item를 옮길 수 있다.

 

◎TableViewController.swift

import UIKit

var items = ["책 구매", "철수와 약속", "스터디 준비하기"]
var itemImageFile = ["cart.png", "clock.png", "pencil.png"]

class TableViewController: UITableViewController {

    @IBOutlet var tbListView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.leftBarButtonItem = self.editButtonItem
    }

    // MARK: - Table view data source
    // 보통 테이블 안에 섹션이 한개이기 때문에 numberOfSections 는 1을 return 하도록 작성
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    // 섹션당 열의 개수는 items의 개수로
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return items.count
    }

    // 주석되어 있던 함수: Table Cell을 채우기 위한 함수
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // table view 내부 Cell의 identifier로 수정
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
        
        // 셀의 텍스트 레이블에 앞에서 선언한 items을 대입한다.
        cell.textLabel?.text = items[(indexPath as NSIndexPath).row]
        cell.imageView?.image = UIImage(named: itemImageFile[(indexPath as NSIndexPath).row])

        // Configure the cell...

        return cell
    }
    
    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    
    // Override to support editing the table view.
    // 주석되어 있던 함수: Table Cell을 삭제하기 위한 함수
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            items.remove(at: (indexPath as NSIndexPath).row)
            itemImageFile.remove(at: (indexPath as NSIndexPath).row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    
    // Delete 버튼의 문구를 수정하는 Override 함수
    override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?
    {
        return "삭제"
    }
    

    
    // Override to support rearranging the table view.
    // 주석되어 있던 함수: 리스트 객체의 순서를 옮기기 위한 함수
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
        let itemToMove = items[(fromIndexPath as NSIndexPath).row]
        let itemImageToMove = itemImageFile[(fromIndexPath as NSIndexPath).row]
        items.remove(at: (fromIndexPath as NSIndexPath).row)
        itemImageFile.remove(at: (fromIndexPath as NSIndexPath).row)
        
        items.insert(itemToMove, at: (to as NSIndexPath).row)
        itemImageFile.insert(itemImageToMove, at: (to as NSIndexPath).row)
    }

}

5. 새 목록 추가하기

 

이제 Table View Controller에서 Add Button을 터치했을 때, AddViewContoller로 넘어와서 새 목록을 추가할 수 있도록 AddViewController.swift 파일을 수정할 것이다.

 

사실 별 거 없다. 

 

아래와 같이 TextField의 값을 Add 버튼을 눌렀을 때, 목록에 추가해주고 pop 해주면 된다.

 

◎AddViewController.swift

import UIKit

class AddViewController: UIViewController {

    @IBOutlet var tfAddItem: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    @IBAction func btnAddItem(_ sender: UIButton) {
        items.append(tfAddItem.text!)
        itemImageFile.append("clock.png")
        tfAddItem.text = ""
        
        _ = navigationController?.popViewController(animated: true)
    }
    
}

 

AddViewController에서 목록 Item을 추가는 하지만 따로 TableList 객체를 업데이트 하는 것은 아니기 때문에 TableViewController에서 TableViewContoller가 다시 view 되었을 때, 호출하는 viewWillAppear 함수를 재정의하여 테이블을 리로드할 필요가 있다.

 

◎TableViewController.swift

import UIKit

var items = ["책 구매", "철수와 약속", "스터디 준비하기"]
var itemImageFile = ["cart.png", "clock.png", "pencil.png"]

class TableViewController: UITableViewController {

    @IBOutlet var tbListView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.leftBarButtonItem = self.editButtonItem
    }

    // MARK: - Table view data source
    // 보통 테이블 안에 섹션이 한개이기 때문에 numberOfSections 는 1을 return 하도록 작성
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    // 섹션당 열의 개수는 items의 개수로
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return items.count
    }

    // 주석되어 있던 함수: Table Cell을 채우기 위한 함수
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // table view 내부 Cell의 identifier로 수정
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
        
        // 셀의 텍스트 레이블에 앞에서 선언한 items을 대입한다.
        cell.textLabel?.text = items[(indexPath as NSIndexPath).row]
        cell.imageView?.image = UIImage(named: itemImageFile[(indexPath as NSIndexPath).row])

        // Configure the cell...

        return cell
    }
    
    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    
    // Override to support editing the table view.
    // 주석되어 있던 함수: Table Cell을 삭제하기 위한 함수
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            items.remove(at: (indexPath as NSIndexPath).row)
            itemImageFile.remove(at: (indexPath as NSIndexPath).row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    
    // Delete 버튼의 문구를 수정하는 Override 함수
    override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?
    {
        return "삭제"
    }
    

    
    // Override to support rearranging the table view.
    // 주석되어 있던 함수: 리스트 객체의 순서를 옮기기 위한 함수
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
        let itemToMove = items[(fromIndexPath as NSIndexPath).row]
        let itemImageToMove = itemImageFile[(fromIndexPath as NSIndexPath).row]
        items.remove(at: (fromIndexPath as NSIndexPath).row)
        itemImageFile.remove(at: (fromIndexPath as NSIndexPath).row)
        
        items.insert(itemToMove, at: (to as NSIndexPath).row)
        itemImageFile.insert(itemImageToMove, at: (to as NSIndexPath).row)
    }
    
    // 이 테이블 뷰가 다시 show됐을 때
    override func viewWillAppear(_ animated: Bool) {
        // table list reload
        tbListView.reloadData()
    }

}

6. 목록의 세부 내용 보기

 

이제 Cell item을 탭하여 DetailViewController로 넘어와 세부 내용을 보여주는 코드를 작성해주자.

 

DetailViewContoller는 마찬가지로 별 내용 없이 receiveItem 이라는 String 변수를 하나 초기화 하고, 이를 TableViewController에서 받아 가져오기만 한다.

 

◎DetailViewContoller.swift

import UIKit

class DetailViewController: UIViewController {

    var receiveItem = ""
    
    @IBOutlet var lblItem: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        lblItem.text = receiveItem
    }
    
    func receiveItem(_ item: String) {
        receiveItem = item
    }
   
}

 

단, DetailView로 이동하는 Segue way 는 identifier로 구분했기 때문에 아래와 같이 TableViewController에서 prepare 메서드를 통해 sgDetail로 가는 요청인 지 확인하여 receiveItem 메서드를 호출해주는 코드를 추가해야 한다.

 

◎TableViewController.swift

import UIKit

var items = ["책 구매", "철수와 약속", "스터디 준비하기"]
var itemImageFile = ["cart.png", "clock.png", "pencil.png"]

class TableViewController: UITableViewController {

    @IBOutlet var tbListView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.leftBarButtonItem = self.editButtonItem
    }

    // MARK: - Table view data source
    // 보통 테이블 안에 섹션이 한개이기 때문에 numberOfSections 는 1을 return 하도록 작성
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    // 섹션당 열의 개수는 items의 개수로
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return items.count
    }

    // 주석되어 있던 함수: Table Cell을 채우기 위한 함수
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // table view 내부 Cell의 identifier로 수정
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
        
        // 셀의 텍스트 레이블에 앞에서 선언한 items을 대입한다.
        cell.textLabel?.text = items[(indexPath as NSIndexPath).row]
        cell.imageView?.image = UIImage(named: itemImageFile[(indexPath as NSIndexPath).row])

        // Configure the cell...

        return cell
    }
    
    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    
    // Override to support editing the table view.
    // 주석되어 있던 함수: Table Cell을 삭제하기 위한 함수
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            items.remove(at: (indexPath as NSIndexPath).row)
            itemImageFile.remove(at: (indexPath as NSIndexPath).row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    
    // Delete 버튼의 문구를 수정하는 Override 함수
    override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?
    {
        return "삭제"
    }
    

    
    // Override to support rearranging the table view.
    // 주석되어 있던 함수: 리스트 객체의 순서를 옮기기 위한 함수
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
        let itemToMove = items[(fromIndexPath as NSIndexPath).row]
        let itemImageToMove = itemImageFile[(fromIndexPath as NSIndexPath).row]
        items.remove(at: (fromIndexPath as NSIndexPath).row)
        itemImageFile.remove(at: (fromIndexPath as NSIndexPath).row)
        
        items.insert(itemToMove, at: (to as NSIndexPath).row)
        itemImageFile.insert(itemImageToMove, at: (to as NSIndexPath).row)
    }
    
    // 이 테이블 뷰가 다시 show됐을 때
    override func viewWillAppear(_ animated: Bool) {
        // table list reload
        tbListView.reloadData()
    }

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    // Segue Way를 이용하여 View를 이동하는 함수
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
        if segue.identifier == "sgDetail" {
            let cell = sender as! UITableViewCell
            let indexpath = self.tbListView.indexPath(for: cell)
            
            let detailView = segue.destination as! DetailViewController
            
            detailView.receiveItem(items[((indexpath! as NSIndexPath).row)])
        }
    }
    

}

 

이렇게 하면 Table View Contoller 예제는 모두 완성했다.

 

결과를 확인해보면,

 

아주 잘 동작하는 것을 확인할 수 있다.

'Mobile > ios' 카테고리의 다른 글

[ios] ios 13: 비디오 재생  (0) 2022.12.22
[ios] ios 12: 음악 재생 & 녹음  (0) 2022.12.21
[ios] ios 10: Navigation Controller  (0) 2022.12.19
[ios] ios 9: Tab Bar  (0) 2022.12.17
[ios] ios 8: Page Control  (0) 2022.12.17
Comments