
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}

ComposeViewController에다가 extension을 추가해주자(주파수 이름 설정)
extension ComposeViewController {
static let newMemoDidInsert = Notification.Name(rawValue: "newMemoDidInsert")
}
또, save 코드에 notificationCenter를 추가해준다.
@IBAction func save(_ sender: Any) {
// memoTextView에 있는 text를 newMemo로 저장!
guard let memo = memoTextView.text, memo.count > 0 else {
alert(message: "메모를 입력하세요")
return
}
let newMemo = Memo(content: memo)
Memo.dummyMemoList.append(newMemo)
// NotificationCenter Post 설정하기
NotificationCenter.default.post(name: ComposeViewController.newMemoDidInsert, object: nil)
// 새 메모 창을 닫기
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// queue -> 어느 쓰레드에서 실행할 것인가? 무조건 main, 뒤의 클로저를 queue에서 실행한다!
NotificationCenter.default.addObserver(forName: ComposeViewController.newMemoDidInsert, object: nil, queue: OperationQueue.main) { [weak self] (noti) in
self?.tableView.reloadData()
}
}
그리고, 메모리 낭비를 피하기 위하여 옵져버를 해제해보자
var token: NSObjectProtocol?
deinit {
if let token = token {
NotificationCenter.default.removeObserver(token)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// queue -> 어느 쓰레드에서 실행할 것인가? 무조건 main, 뒤의 클로저를 queue에서 실행한다!
token = NotificationCenter.default.addObserver(forName: ComposeViewController.newMemoDidInsert, object: nil, queue: OperationQueue.main) { [weak self] (noti) in
self?.tableView.reloadData()
}
}
새로운 viewController를 생성해준다.
그리고 cell을 선택하여 새로 생성한 viewController에 show 로 이어준다.

그리고 cell을 선택하고 Accessory를 Disclosure Indicator로 설정을 해주면

아래와 같이 > 가 생기게 된다.

새로운 viewController class를 만들고, view controller와 연결해준다.
그리고 아래와 같이 DetailViewController에 TableView와ToolBar를 생성해준다.

그리고 Table View Cell의 style을 basic으로 바꾸고 identifier를 memoCell로 바꾼다.

TableView를 선택하고 아래와 같이 Prototype cells를 2로 해주면 cell이 두개 생성이된다.

아래와 같이 각각의 cell의 이름을 memoCell, dateCell로 설정해준다.



DetailViewController에 tableViewDateSource를 설정해주자.

Fix를 누르면 아래와 같이 설정이된다.

cellForRowAt는 아래와 같은 코드로 구성한다.
extension DetailViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row{
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "memoCell", for: indexPath)
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "dateCell", for: indexPath)
return cell
default:
fatalError()
}
}
}
var memo: Memo?
'prepare 함수'
segue가 연결된 화면을 생성하고, 화면을 전환하기 직전에 호출이 된다. sender -> 앞선 tableviewcell을 받는다.
// segue가 연결된 화면을 생성하고, 화면을 전환하기 직전에 호출이 된다. sender -> cell을 받는다.
// 아래 코드는 Detailview의 memo 변수를 설정하는 과정이다.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let cell = sender as? UITableViewCell, let indexPath = tableView.indexPath(for: cell){
// destination -> 새로 전환되는 화면(DetailView)
if let vc = segue.destination as? DetailViewController {
vc.memo = Memo.dummyMemoList[indexPath.row]
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row{
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "memoCell", for: indexPath)
cell.textLabel?.text = memo?.content
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "dateCell", for: indexPath)
cell.textLabel?.text = formatter.string(for: memo?.insertDate)
return cell
default:
fatalError()
}
}
아래와 같이 tableview의 selection을 no selection, separator은 None으로 설정을 한다.

그리고 memoCell의 textlabel 설정을 변경하면, Lines를 0으로 하여 여러 줄이 나올 수 있도록 설정하고, line break를 word wrap으로 하여 단어 묶음으로 줄 바꿈을 할 수 있도록 설정을 한다.
