Trulyawesome

TodoApp 제작(Swift)2 본문

프로그래밍/iOS

TodoApp 제작(Swift)2

ethkelitt 2020. 1. 24. 15:41

-구현기능

1.Todo 입력

2.Todo 삭제

3.Todo 자세히 보기

4.달력보기

5.달력 일자 다중선택

 

-사용기술

1.TableView

2.Swift 5.0

3.MVC

4.Koyomi(Calendar)

5.Toolbar

6.navigation controller

 

선행되어할것들 (기존 컨트롤뷰를 지우고 네비게이션 컨드롤 뷰를 만들어준다. )

 

1.TableView 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
extension TodoViewController: UITableViewDataSource {
    
    //cell count
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int-> Int {
        return todoList.count
    }
    
    //cell present
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell"for: indexPath) as? TodoCell else {return UITableViewCell()}
        
        cell.title.text = todoList[indexPath.row].title
        
        return cell
    }
cs

1편과 같이 먼저 tableView를 만들어 주시고 DataSource 설정은 위와 같이 만들어 봤습니다. 요점은 todoList에 저장한 제목과 내용을 불러오는 것입니다. 

 

2. Todo 입력

Todo입력 화면

위와 같이 textField와 textView를 사용하였습니다 textField에는 제목을 textView에는 내용을 담아줍니다. 그리고 Done 버튼을 누르면 Todo 입력이 완료됩니다. 

1
2
3
4
5
6
7
8
9
10
@IBAction func doneButton(_ sender: Any) {
        let title = titleText.text ?? ""
        let content = contentText.text ?? ""
        
        let item: Todo = Todo(title: title, contents: content)
 
        todoList.append(item)
        
        self.navigationController?.popViewController(animated: true)
    }
cs

done버튼은 위와같이 구현해줍니다. 입력이 완료되면 todoList로 저장됩니다. 그리고  self.navigationController?.popViewController(animated: true)를 호출하여 네비게이션 초기화면으로 이동해줍니다, 

하지만 입력한 todo 정보가 보이지 않습니다. 이것을 해결하기 위해서는 view를 다시 reload를 해주어야합니다

1
2
3
4
override func viewDidAppear(_ animated: Bool) {
        TodoListView.reloadData()
        saveAllData()
    }
cs

위와 같이 viewDidAppear 함수를 작성하여 TodoListView를 reloadData해줍니다. 이렇게 하면 작성한 TodoList가 바로 갱신됩니다. 

2.Todo 삭제

 

delete

swipe delete를 사용하여 데이터 삭제기능을 구현할겁니다. 이것을 구현하기 위해서는 

1
2
3
4
5
6
7
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == UITableViewCell.EditingStyle.delete {
            todoList.remove(at: indexPath.row)
            TodoListView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
            TodoListView.reloadData()
        }
    }
cs

tableView DataSource extension안에 위와 같은 함수를 입력해주면 간단하게 구현이 됩니다. 데이터 갱신을 위해 reloadData는 꼭 추가해 주도록 합시다 

'프로그래밍 > iOS' 카테고리의 다른 글

ViewController Life Cycle  (0) 2020.05.06
iOS 로그인 화면 구성  (0) 2020.02.08
Swift Userdefault로 데이터 저장하기  (0) 2020.01.21
TodoApp 제작(Swift)1  (0) 2020.01.18
Firebase 데이터 입력 및 불러오기  (0) 2019.12.29