์๋ ํ์ธ์~
์ค๋์ Cell์์ ์๋ ๋ฒํผ์๊ฒ ์ด๋ฒคํธ๋ฅผ ์ค ์ ์๋ ๋ฐฉ๋ฒ์ ๋ํด์ ์์ฑํ๋ ค๊ณ ํฉ๋๋ค : )
์ ์์ ์๋ ๋ฒํผ์ ์ด๋ฒคํธ๋ฅผ ์ฃผ๋ ๋ฐฉ๋ฒ์๋
Tag ๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ, Delegate๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ ๋ฑ ๋ค์ํ ๋ฐฉ๋ฒ์ด ์กด์ฌํฉ๋๋ค.
๊ทธ์ค์์ ์ค๋ ์๊ฐํ ๋ฐฉ๋ฒ์ ํด๋ก์ ๋ฅผ ์ด์ฉํ ์ด๋ฒคํธ ์ฒ๋ฆฌ๋ฐฉ๋ฒ์ ๋๋ค.
1. ๋จผ์ ์์๋์ด์ผ ํ ๊ฒ
- ์ ์์ ๋ฒํผ๊ณผ ๊ฐ์ด ์ ์ ์์ ์ธํฐ๋ ์ ์ด ํ์ํ ๊ฒฝ์ฐ ์ ์ ContentView์ ํด๋น ๋ทฐ๋ฅผ ๋ฃ์ด์ค์ผ ํฉ๋๋ค.
//TableViewCell
class FindFriendsTableViewCell: UITableViewCell {
static let identifier = "FindFriendsTableViewCell"
let infoView = MyInfoView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//1. ContentView์ ๋ฃ์ด์ฃผ๊ธฐ
contentView.addSubview(infoView)
infoView.snp.makeConstraints { make in
make.top.equalToSuperview()
make.width.equalToSuperview().multipliedBy(0.9)
make.centerX.equalToSuperview()
make.height.equalTo(510)
}
}
override func layoutSubviews() {
super.layoutSubviews()
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 15, left: 0, bottom: 0, right: 0))
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
๋ณด์ด๋ ๊ฒ์ฒ๋ผ ContentView์ ๋ฃ์ด์ฃผ์ด์ผ ์ ์์ ์ผ๋ก ์ ์ ์์ ์ธํฐ๋ ์ ์ด ์ด๋ฃจ์ด์ง ์ ์์ต๋๋ค. ์๋ํ๋ฉด ํด๋น ViewController์ hierarchy๋ฅผ ํ์ธํด ๋ณด๋ฉด ์ ๋ค์์ผ๋ก ์์ด๊ฒ ๋๋ ๊ฒ ContentView์ด๊ธฐ ๋๋ฌธ์ ๋๋ค.
( ๊ฐํน ๊ทธ๋๋ ๋์ํ์ง ์๋๋ค๋ฉด TableView์ UserIntercationEnabled ๊ฐ์ด false์ธ์ง ํ์ธํด์ฃผ์๊ณ false๋ผ๋ฉด true๋ก ๋ณ๊ฒฝํด ์ฃผ์ธ์. )
2. ๋ฒํผ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํด์ค ํด๋ก์ ๋ง๋ค๊ธฐ
//TableViewCell
class FindFriendsTableViewCell: UITableViewCell {
static let identifier = "FindFriendsTableViewCell"
let infoView = MyInfoView()
//2. ๋ฒํผ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํด์ค ํด๋ก์ ๋ง๋ค๊ธฐ
var buttonAction : (() -> Void) = {}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//1. ContentView์ ๋ฃ์ด์ฃผ๊ธฐ
contentView.addSubview(infoView)
infoView.snp.makeConstraints { make in
make.top.equalToSuperview()
make.width.equalToSuperview().multipliedBy(0.9)
make.centerX.equalToSuperview()
make.height.equalTo(510)
}
}
override func layoutSubviews() {
super.layoutSubviews()
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 15, left: 0, bottom: 0, right: 0))
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
์์ ๊ฐ์ด ํด๋ก์ ํ๋กํผํฐ๋ฅผ ์ ์ธํด์ฃผ์๊ณ ๋์
3. @objc ๋ฉ์๋ ๋ง๋ค๊ณ ๋ฒํผ์์ addTarget ์ค์ ํ๊ธฐ
//TableViewCell
class FindFriendsTableViewCell: UITableViewCell {
static let identifier = "FindFriendsTableViewCell"
let infoView = MyInfoView()
//2. ๋ฒํผ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํด์ค ํด๋ก์ ๋ง๋ค๊ธฐ
var buttonAction : (() -> Void) = {}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//1. ContentView์ ๋ฃ์ด์ฃผ๊ธฐ
contentView.addSubview(infoView)
infoView.snp.makeConstraints { make in
make.top.equalToSuperview()
make.width.equalToSuperview().multipliedBy(0.9)
make.centerX.equalToSuperview()
make.height.equalTo(510)
}
//3. addTarget ์ค์ ํด์ฃผ๊ธฐ
infoView.button.addTarget(self, action: #selector(requestFriend), for: .touchUpInside)
}
override func layoutSubviews() {
super.layoutSubviews()
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 15, left: 0, bottom: 0, right: 0))
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc
func requestFriend() {
buttonAction()
}
}
4. TableView๋ฅผ ๊ด๋ฆฌํ๋ ViewController์์ ์ ์ฉํ๊ธฐ
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: FindFriendsTableViewCell.identifier, for: indexPath) as! FindFriendsTableViewCell
//4.ํด๋ก์ ๊ตฌ๋ฌธ์ ์ด๋ฒคํธ๋ฅผ ๋ฃ์ด์ค๋๋ค
cell.buttonAction = {
self.requestFriend(self.viewModel.friends[indexPath.row].uid)
}
return cell
}
์ด๋ ๊ฒ ํ๋ฉด ์ ์์ ์ผ๋ก ์ด๋ฒคํธ๋ฅผ ์คํํฉ๋๋ค.
์ถ๊ฐ์ ์ผ๋ก ์ ์ ์ฌ์ฌ์ฉ์ ์ธ ์ธก๋ฉด ๋๋ฌธ์ ๊ธฐ์กด์ ์ด๋ฒคํธ๋ฅผ nil ํน์ ๋น๊ฐ์ผ๋ก ์ฒ๋ฆฌ๋ฅผ ํด์ผ ํฉ๋๋ค.
๋ฌผ๋ก , ๊ฐ์ ๋ฉ์๋๋ฅผ ํธ์ถํด์ผ ํ๋ ๊ฒ์ด๋ผ๋ฉด ์ฌ์ฉํ์ง ์์๋ ๋ฉ๋๋ค.
๋ค์์ Cell์ ์ด๋ฒคํธ๋ฅผ nil ์ฒ๋ฆฌํด ์ฃผ๋ ์ฝ๋์ ๋๋ค.
5. ์ด๋ฒคํธ ์ด๊ธฐํ ํ๊ธฐ
//TableViewCell
class FindFriendsTableViewCell: UITableViewCell {
static let identifier = "FindFriendsTableViewCell"
let infoView = MyInfoView()
//2. ๋ฒํผ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํด์ค ํด๋ก์ ๋ง๋ค๊ธฐ
var buttonAction : (() -> Void) = {}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//1. ContentView์ ๋ฃ์ด์ฃผ๊ธฐ
contentView.addSubview(infoView)
infoView.snp.makeConstraints { make in
make.top.equalToSuperview()
make.width.equalToSuperview().multipliedBy(0.9)
make.centerX.equalToSuperview()
make.height.equalTo(510)
}
//3. addTarget ์ค์ ํด์ฃผ๊ธฐ
infoView.button.addTarget(self, action: #selector(requestFriend), for: .touchUpInside)
}
override func layoutSubviews() {
super.layoutSubviews()
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 15, left: 0, bottom: 0, right: 0))
}
// 4.
override func prepareForReuse() {
super.prepareForReuse()
buttonAction = {}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc
func requestFriend() {
buttonAction()
}
}
์ด๋ ๊ฒ ํ๋ฉด ์ ์์ ๋ฒํผ ์ด๋ฒคํธ๋ฅผ ์ค ์ ์์ต๋๋ค.
๋ ์ข์ ๋ฐฉ๋ฒ์ด๋
์์ ์์ฑ ๋ด์ฉ์ด ์๋ชป๋์์ ๊ฒฝ์ฐ, ๋๊ธ ๋ถํ๋๋ฆฝ๋๋ค.
๊ฐ์ฌํฉ๋๋ค : - )
๊ทธ๋ผ ์ด๋ง ๐๐ป ๐๐ป ๐๐ป
'iOS > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[swift] UILabel intrinsicContentSize ํ์ฉ๋ฐฉ๋ฒ (0) | 2022.02.23 |
---|---|
[swift] NavigationController์ setViewController ์ฌ์ฉ๋ฐฉ๋ฒ (0) | 2022.02.18 |
[swift] Timer๋ฅผ ํ์ฉํ API ๋ฐ๋ณตํธ์ถ ๋ฐฉ๋ฒ (0) | 2022.02.16 |
[swift] SnapKit์ ๋ํด์ ( + update Constraints ์ด์) (0) | 2022.02.15 |
[Swift] UICollectionView์์ Cell์ ์ผ์ชฝ ์ ๋ ฌ(Left Alignment) ํ๋ ๋ฐฉ๋ฒ (0) | 2022.02.14 |