iOS

[ iOS ] contentConfiguration: TableView์— ๊ธฐ๋ณธ Cell ์‚ฌ์šฉํ•˜๊ธฐ (iOS 14+)

Forest Yun 2022. 11. 29. 13:42
728x90

๊ณต๋ถ€ ๊ธฐ๋ก

 

 

 

 

 

 

 

 

UITableViewCell์€ ์ปค์Šคํ…€ํ•˜์ง€ ์•Š๊ณ ๋„ cell์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ์—ฌ๋Ÿฌ ์˜ต์…˜์„ ์ œ๊ณตํ•œ๋‹ค. ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” UITableViewCell์˜ ํ”„๋กœํผํ‹ฐ ์ค‘ textLabel, detailTextLabel, imageView๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด์˜€์ง€๋งŒ, iOS 14 ์ดํ›„๋ถ€ํ„ฐ๋Š” ์ด ํ”„๋กœํผํ‹ฐ๋“ค์€ Deprecated  ๋˜์–ด ๋” ์ด์ƒ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ์ด๊ฒƒ๋“ค์„ ๋Œ€์ฒดํ•˜๋Š” ํ”„๋กœํผํ‹ฐ๊ฐ€ ๊ฐœ๋ฐœ๋˜์—ˆ๋Š”๋ฐ, ๋ฐ”๋กœ  contentConfiguration ์ด๋‹ค.

 

UIButton์—์„œ๋„ ์ด์™€ ์œ ์‚ฌํ•œ ํ”„๋กœํผํ‹ฐ์ธ configuration(iOS 15+)์ด ์žˆ๋‹ค. ๋งŽ์€ Class๋“ค์—์„œ Configuration์ด ๋“ฑ์žฅํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์•„ ๊ฐœ๋ฐœ์ž๊ฐ€ View ๊ตฌ์„ฑ์„ ํ•œ ๊ฐ์ฒด๋กœ ์ •๋ฆฌ๋˜๋ฉฐ ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ ํ˜•ํƒœ๋กœ ๊ฐœ๋ฐœํ•˜๋„๋ก Apple์—์„œ  ๋ฐฉํ–ฅ์„ฑ์„ ์ œ์‹œํ•ด์ฃผ๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

 

 

 

 

Deprecated ์—์„œ๋„ ์ž˜ ๋‚˜์™€์žˆ๋“ฏ์ด  contentConfiguration์˜ ์‚ฌ์šฉ์€ ๊ฐ„๋‹จํ•˜๋‹ค.

Deprecated
cell์˜ text๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด content configuration์„ ๋Œ€์‹  ์‚ฌ์šฉํ•˜์„ธ์š”. ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต๋˜๋Š” list content configuration์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” defaultContentConfiguration()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฐ˜ํ™˜๋œ configuration์˜ text ํ”„๋กœํผํ‹ฐ๋ฅผ ํ†ตํ•ด ์ดˆ๊ธฐ text๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋ฅผ cell์— ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” cell์˜ contentConfiguration ํ”„๋กœํผํ‹ฐ์— ํ• ๋‹นํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = UITableViewCell()
        var content = cell.defaultContentConfiguration()
        
        content.text = "\(indexPath.row + 1)" //์ œ๋ชฉ text
        content.secondaryText = "\(indexPath.row + 1)๋ฒˆ cell" //์„ค๋ช… text
        content.image = .init(systemName: "person.fill") //์ด๋ฏธ์ง€
        content.imageProperties.tintColor = .darkGray //์ด๋ฏธ์ง€ ์ƒ‰์ƒ
        
        cell.contentConfiguration = content //cell์— configuration ์ ์šฉ
        
        return cell
    }

 

 

 

 

 

 

 

 

UITableViewCell.contentConfiguration
UITableViewCell.defaultContentConfiguaration
UITableViewCell.textLabel
728x90