iOS/Swift

[swift] TableViewCell/CollectionViewCell ์…€์•ˆ์˜ ๋ฒ„ํŠผ์— ์ด๋ฒคํŠธ๋ฅผ ์ฃผ๋Š” ๋ฐฉ๋ฒ•

๊ฒฝํ‘ธ 2022. 2. 17. 21:38
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”~

 

์˜ค๋Š˜์€ 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()
    }
}

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์…€์•ˆ์— ๋ฒ„ํŠผ ์ด๋ฒคํŠธ๋ฅผ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋” ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋‚˜ 

์œ„์˜ ์ž‘์„ฑ ๋‚ด์šฉ์ด ์ž˜๋ชป๋˜์—ˆ์„ ๊ฒฝ์šฐ, ๋Œ“๊ธ€ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค : - )

 

๊ทธ๋Ÿผ ์ด๋งŒ ๐Ÿ‘‹๐Ÿป ๐Ÿ‘‹๐Ÿป ๐Ÿ‘‹๐Ÿป

๋ฐ˜์‘ํ˜•