포스트

Todoey (1)

TableView 만드는것은 생략.

Cell 선택했을때 회색화면 잠깐 보였다가 사라지게 하기.

1
2
3
4
5
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(itemArray[indexPath.row])
        
        tableView.deselectRow(at: indexPath, animated: true) // new
    }
  • before

  • after

Cell 옆에 체크마크 표시하기

1. StoryBoard

2. Function

didSelectRowAt 함수에 적을 것이다.

특정 인덱스 경로에 대한 셀정보를 가져오기위해 다음과 같이 적는다

tableView.cellForRow(at: indexPath)

1
2
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
                                   ----------------------------

다음과 같은 부분을 추가해준다.

그리고 실행해보면?

일단 누르면 체크표시가 뜨는건 된다.

하지만 다시 클릭했을때 사라져야하는데 그것은 아직 되지 않는다.

3 해당 문제 해결하기

1
2
3
4
5
6
7
if tableView.cellForRow(at: indexPath)?.accessoryType == .checkmark {
            
    tableView.cellForRow(at: indexPath)?.accessoryType = .none
            
    } else {
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}

현재 체크가 된것이라면 none으로, 그게 아니라면 체크하라는 내용을 추가하자.

잘된다.

Bar Button 기능구현하기.

추가하는 부분은 생략.

1. UIAlertController 호출하기.

UIAlertController를 호출하여 기능을 구현해준다.

1
2
3
4
5
6
7
8
9
10
11
12
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
        
        let alert = UIAlertController(title: "Add New Todoey Item", message: "", preferredStyle: .alert)
        
        let action = UIAlertAction(title: "Add Item", style: .default) { (action) in
            // what will happen once the user clicks the Add Item button on our UIAlert
            print("Success!")
        }
        alert.addAction(action)
        
        present(alert, animated: true, completion: nil)
    }

잘 나온다.

2. Alert에 TextField 추가하기

1
2
3
 alert.addTextField { (alertTextField) in
            alertTextField.placeholder = "Create new item"
        }

3. TextField에 입력한 값 출력하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
        
        var textField = UITextField()
        
        let alert = UIAlertController(title: "Add New Todoey Item", message: "", preferredStyle: .alert)
        
        let action = UIAlertAction(title: "Add Item", style: .default) { (action) in
            // what will happen once the user clicks the Add Item button on our UIAlert
            print(textField.text) // new
            
        }
        
        alert.addTextField { (alertTextField) in
            alertTextField.placeholder = "Create new item"
            textField = alertTextField
        }
        
        alert.addAction(action)
        
        present(alert, animated: true, completion: nil)
    }

혹시라도 현재 추가한 부분을 alert.addTextField나 다른부분에 추가할 경우 해당 값을 받아 올 수 없다.

왜냐하면 지금 아래 부분은 alert를 표현하는 부분이다. 즉 행동 이후에 대한 내용이 아닌, 행동과정에 대한 부분이다.

우리가 textfield를 입력한 이후의 시점에 대해선 action 오브젝트 내부에서 실행이 되어야 한다.

4. TextField 추가한값을 배열에 저장하기

print문 대신 append를 사용하여 추가하자.

하지만 값을 추가해도 현재 보이지는 않는다.

1. Break Point 사용

현재 추가하는 부분에 Break Point를 주었다.

그리고 추가를 하면

내가 설정한 부분에서 작동이 멈추고 이렇게 바뀐다.

lldb 콘솔에 itemArray를 확인해보면?

이렇게 나온다

이때 step over 를 해주고 다시 print 해보면?

값이 추가되어있는걸 알 수 있다.

단지 TableView에 보이지만 않을 뿐이다.

그래서 값을 추가하고 다시 불러오기 위해서 tableView.reloadData()를 배열 추가한 부분 뒤에 적어주자.

잘 나온다.

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.