๐ ๋ฌธ์ ์ํฉ: TableView๋ฅผ ์ด์ฉํด์ cell์ ๋ง๋ค๋ฉด ์ข์ ์ ์ ๊ฐ์ ๊ฑธ ์ฌ๋ฌ ๋ฒ ๋ฐ๋ณตํด์ ๋ง๋ค ํ์๊ฐ ์๋ค๋ ๊ฒ์ด๊ณ , ์๊ฐํด์ผ ํ ๋ถ๋ถ์ ๊ทธ ์ผ๊ดํ๊ฒ ๋ฐ๋ณต๋๋ ์ ์ ์ด๋ป๊ฒ ๊ตฌ๋ณํ ๊ฒ์ธ์ง ๊ฐ๋ค. ๊ทธ๋ฆฌ๊ณ ์ฌ์ ํ indexPath์ ํ์ฐ์ ๋๋ ๋ด๊ฐ ์ง๋ฉดํ... TableViewCell์ ์ถ๊ฐํ ๋ฒํผ ์ค ํน์ index์ ๋ฒํผ์ ์ด๋ฒคํธ๋ฅผ ์ฃผ๋ ค๋๋ฐ (ํ์ ์ฐฝ์ ๋ง๋ค๊ณ ์๋์ค์ด์๋ค) ์ด์งธ์์ธ์ง indexPathํ์ฉ์ด ๋์ง ์๋ ๊ณตํฌ. ์กฐ๊ฑด๋ฌธ์ ๊ฐ์ฅ ์์ ์๊ฒ ์ธ ์ค ์์์ ์ค๋ง ์กฐ๊ฑด๋ฌธ์ ์ฐ๋ฉด ์๋๋? ํ๋ ์๊ฐ๋ถํฐ ์ฌ๋ฌ ๋ฒ ์์ ์ ํ์ง๋ง '์ ' ์์ 'ํน์ ํ ๋ฒํผ' ํด๋ฆญ์ด ์ ๋์ง ์์๋ค. ๊ทธ๋ ๊ฒ ์ฝ์ง์ด ์์ ๋์๋ค.
โ๏ธ ํด๊ฒฐ๋ฐฉ์: ๊ตฌ๊ธ๋ง์ ๊ตฌ๊ธ๋ง์ ๊ตฌ๊ธ๋ง์ ๊ตฌ๊ธ๋ง.... ์ ํ๋ค๊ฐ (delegate๊ณผ clouser์๋? ์ด ๋๊ฐ์ง ๋ฐฉ์์ด ๊ฐ์ฅ ๋ง์ด ๋์์ง๋ง ๋ฐ๋ผํ๋ค๊ฐ ์จ๊ฐ ์ค๋ฅ์์ง์ ํด์ ๋ ์ฌ์ด ๋ฐฉ๋ฒ์ ์ฐพ์ํค๋งธ๋ค) ์ด๋ด ๋ indexPath๊ฐ ์๋๋ผ (์ด๋ถ๋ถ์ ์ข ๋ ์ฐพ์๋ด์ผํ ๊ฒ ๊ฐ๋ค) tag ๊ธฐ๋ฅ์ ์จ์ผํ๋ค๋ ๊ฒ์ด์๋ค. ๋ค๋ง, ๋์ค์ ํ๊ทธ์ ์๊ฐ ๋์ด๋๋ฉด ๋์ค์ ๊ทธ๊ฑธ ๋ค~ ์จ์ผํ๊ธฐ ๋๋ฌธ์ ์ด๋ ค์์ด ์๊ธฐ์ ๋น์ถ์ฒ ๋ฐฉ๋ฒ์ด๋ผ๊ณ ๋ง์ด ๋์๋๋ฐ, ํ์ฌ ๋ด๊ฐ ๊ตฌํ์ค์ธ ๊ฑด ๋คํํ ํ๊ทธํ ์๊ฐ ์ ์ผ๋ ์ด ๋ฐฉ๋ฒ์ ์ฐ๊ธฐ๋ก ํ๋ค. ๋๋ฅ. ๊ทธ๋ ๊ฒ ๋จธ๋ฆฟ์์ ์ค์ณ์ง๋๊ฐ๋ ์์
๋ ๋ฐฐ์ด AddTarget๊ณผ @objc func ์ด ์๊ฐ์ด ๋ฌ๊ณ ... ๋ชฉ์ ์ด ์กฐ๊ธ ๋ค๋ฅด์ง๋ง ๋ฒํผ์ ํ๊ทธ ๊ธฐ๋ฅ์ ์ถ๊ฐํด์ ๊ทธ๊ฑธ ํ์ฉํ๋๋ ํด๊ฒฐ์ด ๋์๋ค. ๐
์๋๋ ๊ทธ ๊ณผ์ ์ ์ผ๋ถ๋ฅผ ๋จ๊ธด๋ค.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print(indexPath)
let cell = tableView.dequeueReusableCell(withIdentifier: "SettingTableViewCell") as! SettingTableViewCell
//๋ฐ์ดํฐ ์ด๊ธฐํ ํ์
cell.detailButton.tag = indexPath.row
cell.detailButton.addTarget(self, action: #selector(detailButtonClicked), for: .touchUpInside)
return cell
}
1. ๋จผ์ cellForRowAt ์ cell.๋ง๋ค์ด๋๋ฒํผ์ด๋ฆ.tag = indexPath.row๋ก ํ๊ทธ์ ์ธ๋ฑ์ค๋ฅผ ๋ง์ถฐ์ค๋ค. ๋ indexPath๋ง ์ฐ๋ ค๊ณ ํ์ผ๋ ๊ฒฐ๊ณผ๊ฐ ๋์ค์ง ์์๋ ๊ฒ.
2. ๊ทธ๋ค์ addTarget์ ๊ทธ ์๋ซ์ค์ ์ถ๊ฐํด์ค๋ค. action์ #selector(๋ฒํผ ํจ์์ด๋ฆ)์ ์ ์ด์ฃผ๊ณ , .touchUpInside๋ก ๋ง์ถฐ์ค๋ค.
//์ด๊ธฐํ๋ฒํผ ๋ง๋ค๊ธฐ
@objc func detailButtonClicked (_ sender: UIButton) {
if sender.tag == 2 {
let alert = UIAlertController(title: "๋ฐ์ดํฐ ์ด๊ธฐํ", message: "์ ๋ง ๋ค์ ์์ํ์ค๊ฑด๊ฐ์?๐ฅบ", preferredStyle: .alert)
let ok = UIAlertAction(title: "์", style:.default)
let cancel = UIAlertAction(title: "์๋์", style: .cancel)
alert.addAction(cancel)
alert.addAction(ok)
present(alert, animated: true)
}
3. ์ด ํํธ๋ tableview ์ชฝ์ ์์ฑํ๋ ๊ฒ ์๋๋ผ, viewController์์ญ์ ์ ์ด์ค์ผํ๋ค. ๊ธธ ์์ง ๋ง๊ฒ.
4. ๊ทธ๋ฆฌ๊ณ ๋๋๊ฒ๋... ์ฌ๊ธด ๋ค ์ง์ ์ ๋ ฅํด์ฃผ์ด์ผ ํ๋ค. _sender ์ด ๋ถ๋ถ๊น์ง๋. ๋จผ์ func ~ ์ด๋ถ๋ถ์ ์จ์ฃผ๊ณ , ์ด ๋ฒํผ์ด ์คํ ๋์์ ๋ ๋ด๊ฐ ์ํ๋ ์ด๋ฒคํธ๋ฅผ ์์ ์ ์ด์ฃผ์๋ค. ๋๋ tableViewCell ๊ธฐ์ค ์ธ๋ฒ์งธ row์ ๋ค์ด๊ฐ ์๋ ๋ฒํผ์ ์ ํํด์ ๊ทธ ๋ฒํผ์ ์ด๋ฒคํธ๋ฅผ ์ฃผ๊ณ ์ถ์๊ธฐ ๋๋ฌธ์ tag๋ฅผ 2๋ก ์ ์๋ค.
5. func ์์ @objc๋ฅผ ๊ผญ ๋ถ์ฌ์ค ๊ฒ. ๊ทธ๋ฆฌ๊ณ ์ฌ๊ธฐ์ ๋ง๋ ํจ์ ์ด๋ฆ(detailButtonClicked)๋ฅผ cellForRowAt์ ์ด AddTarget์ #selector ๊ดํธ ์์ ๋ฃ์ด์ฃผ๋ฉด ๋! ์ด๋ ค์ฐ๋ฉด์๋ ํด์น์ฐ๊ณ ๋๋ ๋๋ฆ ์ฌ์ด ๊ฒ... ๊ฐ๊ธฐ๋?
๊ฒฐ๊ณผ๋ฌผ:
์ฐธ๊ณ : https://stackoverflow.com/questions/20655060/get-button-click-inside-uitableviewcell
'๐ Dev > Issue' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Error] Could not get GOOGLE_APP_ID in Google Services file from build environme (0) | 2023.11.10 |
---|---|
[Error] object has been deleted or invalidated. (0) | 2023.11.01 |
[Issue] Assets์ ์ค์๋ก ์ญ์ ํ๋ค๋ฉด? (0) | 2023.08.22 |
[Error] Could not insert new outlet connection (2) | 2023.08.16 |
[Issue/UIKit] fullScreen vs OverFullScreen (0) | 2023.08.06 |