์ด๋ฒ์ฃผ์๋ Delegate + Protocol + TableView + CollectionView๋ฅผ๋ฐฐ์ ๋น
์ฒ์์ ๋ค์๋ ์ ๋ง ์ดํด๊ฐ 1๋ ๋์ง์๊ณ ์ค๋ฅ๋ฅผ ๋ฟ๋ฟํ๋ xcode๋ฅผ ๋ณด๋ฉฐ ๋๋ฌผ์ด ์ฃผ๋ฅด๋ฅตํ๋ ์ง๋ง,, ์ง์์์ ์ฐจ๋ถํ ๋ง์๊ฐ์ง์ผ๋ก ๋ณต์ตํ๋ฉฐ ๋ค์ ํด๋ณด๋ ์๋ซ๋ค.
๋๋ฆฌ๊ฒ์ดํธ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ ํ๋กํ ์ฝ์ ์์์ผํ๋๋ฐ, ํ๋กํ ์ฝ์ ๊ตฌํ์ ์ํ ์ฒญ์ฌ์ง์ ๋๋์ด์๋ค.
protocol ์ด๋ฆ {
๋ด์ฉ
}
์ด๋ฐ์์ผ๋ก ๊ตฌํ์ ํด์ฃผ๊ณ , ์ด๋ ๋ทฐ์ปจ์๋ค ๋ง๋ค์ด๋ ์ด๋ ๋ทฐ์ปจ์์๋ ํธ์ถ์ด ๊ฐ๋ฅํ๋ค. ์ฐ์ ๋๋ฆฌ๊ฒ์ดํธ๋ฅผ ์จ์ผํ๋ ์ด์ ๋ ๋ฐ์ดํฐ ์ ๋ฌ์ ์ํด์ ์๋๋ฐ, ๋ฐ์ดํฐ ์ ๋ฌ์ ํด์ฃผ๋ ๊ณผ์ ์์์ ํ๋กํ ์ฝ์์ ๋ฐ์ดํฐ ์ ๋ฌ์ ํด์ฃผ๋ ํจ์๋ฅผ ์ ์ธ ํด์ฃผ๊ณ , var delegate๋ผ๋ ๋ฐ์ดํฐ๋ฅผ ์ค ๊ณณ์ ์ ์ธํด์ค๋ค. ๊ทธ ํ, ์ ๋ฌ๋ฐ์ ๋ทฐ ์ปจํธ๋กค๋ฌ์์ ํ๋กํ ์ฝ์ ์ฑํํ๊ณ , ๊ทธ ์ดํ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ ๋ทฐ์ปจ์์ ๋ฐ์ดํฐ๋ฅผ_์ค_๋ทฐ์ปจ.delegate = self (๋ด๊ฐ ๋ท์ฒ๋ฆฌ ํ๊ฒ ๋ค.) ๋ผ๊ณ ์ ์ธ์ ํด์ฃผ๊ณ ๋ฉ์๋๋ฅผ ์ง์ฃผ๊ฒ ๋๋ฉด, ๊ตฌํ๋ ํจ์์์ "ใ ใ ๋ฐ์ดํฐ ๋ฐ์์ ๋ง๋ ํจ์๋๋ก ์ฒ๋ฆฌํ ๊ฒ"ํ๊ณ ์ฒ๋ฆฌํ๋ ๋ฐฉ์์ด๋ค.
์ฒ์์ ์ดํด๊ฐ ์ ์๋ฌ๋ ๋ถ๋ถ์, ๊ทธ๋ฅ ๋ทฐ์ปจ๋ผ๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํด์ฃผ๋ฉด๋์ง ์ ๊ตณ์ด ๋ณต์กํ๊ฒ ๊ผฌ์์ ๋ณด๋ด๋๊ฐ ๋ผ๊ณ ์๊ฐํ์๋๋ฐ, ์ฌ์ค ์ด๊ฑด push๋ present์์๋ ๋ง๋ ๋ง์ด์์ง๋ง, dismiss๋ pop์์๋ ์๋ฒฝํ ํ๋ฆฐ์๊ฐ์ด์๋ค. ๊ฐ์ ๋ณด๋ด๋ ํ์ ์์ฒด๊ฐ push๋ present์์ ์งํ๋๋ ๊ฒ ์ด์๊ธฐ์, ๋ด๊ฐ ์๋ฌด๋ฆฌ ๋ฐ์!! ๋ฐ์ผ๋ผ๊ณ !!! ํด๋ ์๋ฏธ๊ฐ ์๋ ๊ฒ์ด์๋ค. (์ง์ฌ๋์ด๋ค,,,) ๊ทธ๋์ ์ด๋ด๊ฒฝ์ฐ, ๋๋ฆฌ๊ฒ์ดํธ๋ฅผ ํตํด ๊ฐ์ ์ ๋ฌํด์ฃผ๋ฉด ์ ํํ๊ฒ ๊ฐ์ ์ ๋ฌํด ์ค ์ ์๋ค๋ ๊ฒ์ ๊นจ๋ฌ์๋ค.
ํ ์ด๋ธ ๋ทฐ์ ๊ฒฝ์ฐ์๋ ์ฒ์์๋ ํ ์ด๋ธ ๋ทฐ๋ ๋ค๋ฅธ UIView๋ค ์ฒ๋ผ '๋ทฐ'๋ก์จ ๋ณด๋ ๊ด์ ์ผ๋ก ์ ๊ทผํด์ ์ดํด๊ฐ ์กฐ๊ธ ํ๋ค์์ง๋ง,, ๊ตฌ์ ์๋์ ํตํด ์ด์ฌํ ๊ฒ์ํ๋ค๋ณด๋ ํ ๋๊ธ์์ "๋ฐ์ดํฐ๋ฅผ ๋์ดํ๋ ๊ด์ ์ผ๋ก ๋ด๋ผ" ๋ผ๋ ๋ฌธ๊ตฌ๋ฅผ ๋ณด๊ณ ๊นจ๋ณ์๋ค. ์ ๊ทธ๋ฅ ๋ฐ์ดํฐ ์ด์๊ฒ ๋ณด์ฌ์ฃผ๋๊ฑฐ๊ตฌ๋. ๊ทธ๋์ ๊ทธ ์ดํ๋ก๋ ์ ์งค ์ ์๋๊ฒ ๊ฐ๋ค.ํํ.. ์ปฌ๋ ์ ๋ทฐ๋ ํ ์ด๋ธ ๋ทฐ์ ๊ฑฐ์ ํก์ฌํ ๊ฐ๋ ์ธ ๊ฒ ๊ฐ์์ ํ ์ด๋ธ๋ทฐ๋ฅผ ์ดํดํ์ ๊ตฌํํ ์ ์์๋ ๊ฒ ๊ฐ๋ค.
์๋ฌดํผ ๊ทธ๋์ ์ฒ์์๋ ํ ์ด๋ธ ๋ทฐ๋ฅผ ์ด์ฉํด์ ์น๊ตฌ๋ชฉ๋ก์ ๊ตฌํํ๋ ๊ฑฐ์๋ค. ํ ์ด๋ธ ๋ทฐ๋ dequeueReusableCell ์ ์ด์ฉํด์ ๊ตฌํํด์ผ ํ๋ค. ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์์ ๋ทฐ๋ฅผ ์ง๋ ๊ฒ ์ด๊ธฐ๋๋ฌธ์, ๋ช๊ฐ์ ๋ฐ์ดํฐ๊ฐ ์์์ง ์ ํํ ํ์ ์ด ์๋๋ ์ํฉ์์ dequeueReusableCell์ด ๋ฐฉ์์ ์ด์ฉํด์ ๊ตฌํ์ ํด์ผ ํ๋ฐ.
guard let friendsCell = tableView.dequeueReusableCell(withIdentifier: FriendsTableViewCell.identifier, for: indexPath) as? FriendsTableViewCell else {return UITableViewCell() }
CellController์์ ์ ์ ํ๋ํ๋ ๋ฐ์์ค๊ณ , ๊ฐฏ์๋
extension FreindsViewController : UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friendList.count
}
์ผ๋ก ์ธ์ฃผ์ด ๋ฐ์์๋ค. ๊ทธ๋ผ ๊ฐ๊ฐ์ ์ ์ ๋ฐ์ดํฐ๋ ์ด๋ป๊ฒ ๋ฃ์ด์ฃผ๋๋! ๋ผ๊ณ ๋ฌผ์ด๋ณธ๋ค๋ฉด ๋ตํด์ฃผ๋๊ฒ ์ธ์ง์์ .
friendsCell.setData(imageName: friendList[indexPath.row].imageName, Name: friendList[indexPath.row].name, message:friendList[indexPath.row].state)
return friendsCell
friendsCellController์ setDataํจ์๋ฅผ ๋ง๋ค์ด์ฃผ๊ณ ! ์ดํจ์๊ฐ ๊ฐ๊ฐ์ ๋ณ์๋ฅผ ์ง์ด๋ฃ์ ์ ์๋๋ก ํด์ฃผ๋ ์ญํ ์ด๋น.
์ํผ ๊ทธ๋ ๊ฒ ๋๋ฑ๋๋ฑ ํด์ ๋ง๋ค๋ฉด!
์๋ ฟ๊ฒ ๊ตฌํ์ด ๋๋ค. ์ด์ ์ฌ๊ธฐ์ ๊ฐ ์ ์ ๋๋ฌ์ ๋ค๋ฅธ ๋ทฐ์ปจ์ผ๋ก ๋์ด๊ฐ๊ฒ ํ๋ ๋ฒ์!
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
guard let profileVC = storyboard?.instantiateViewController(identifier: "FreindsProfileViewController") as? FreindsProfileViewController else{return}
self.present(profileVC, animated: true, completion: nil)
profileVC.setData(imageName: friendList[indexPath.row].imageName, Name: friendList[indexPath.row].name, state: friendList[indexPath.row].state)
์ผ๋ก didselect๋๋ฉด profileVC๋ฅผ ๋ถ๋ฌ์ฃผ๊ณ , ๊ทธ์์์ ๊ฐ์ ๋ค์ ์ฐพ์์ฌ ์ ์๋๋ก ํด์ฃผ์๋น.
๋๋ฒ์งธ๋, ์ปฌ๋ ์ ๋ทฐ๋ฅผ ๊ตฌํํ๋๊ฑด๋ฐ, ์ปฌ๋ ์ ๋ทฐ์ ๋ ์ด์์์ ์ก๋ ๊ฒ ๋ง๊ณ ๋ ํ ์ด๋ธ ๋ทฐ์ ๋ฐฉ๋ฒ๊ณผ ๋๊ฐ์์ ๊ทธ๋ฅ ์ ๊ตฌํํ๋ค ใ .ใ
๋์ ๊ณผ์ ์ก์ ์ฌํธ๋ ๋ฒํผ ํด๋ฆญ์ actionSheet์ ๋์ฐ๋ ๊ฒ ์ด์๋๋ฐ,
let actionSheet = UIAlertController(title: .none, message: .none, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "ํธ์ง", style: .default, handler: {(ACTION:UIAlertAction) in
}))
์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ ๊ตฌํ์, addAction์ผ๋ก ํด์ฃผ๋ฉด ๋๋๊ฒ์ด๋ค.
๋๋ฒ์งธ ๋์ ๊ณผ์ ๋, ์ฌ๋ผ์ด์ค ํด์ ์ฐจ๋จ,์จ๊น์ด ๋จ๊ฒ ๋ง๋๋ ๊ฒ์ด์๋๋ฐ,
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
์ด ํจ์๋ฅผ ์ด์ฉํด์ ๊ตฌํ ํด ์คฌ๋ค. ์ฐจ๋จ์ ๋๋ ์๋ ์ฌ์ฉ์๊ฐ ๋๋ฅธ ๋ฆฌ์คํธ๊ฐ์ remove(indexPath)ํด์ฃผ์ด์ ๊ทธ ๊ฐ์ ์ง์๋ฒ๋ฆฌ๊ณ ! reload()ํด์ฃผ์ด์ ํ ์ด๋ธ๋ทฐ๋ฅผ ์๋ก๊ณ ์นจ ํด์ฃผ์๋น.
๋ง์ง๋ง ๋์ ๊ณผ์ ๋,,, ์๊ฐ๋ฌธ์ ๋๋ฌธ์ ๊ตฌํํ์ง ๋ชปํ๋ค. ๊ทธ๋์ ์ด๊ฑด ์กฐ๋ง๊ฐ ์๊ฐ์ด ์๊ธฐ๋ฉด ํด๋ณผ์์ ์ด๋ค.
์ํผ ์ด์ ๊ตฌํํ ๋ด์ฉ ์์ฐ์์!