๐Ÿ“‘ Project

[ Project ] ํผ์Šค๋„ ๋ชจ๋นŒ๋ฆฌํ‹ฐ์•ฑ #1 / ์ดˆ๊ธฐ ์™€์ด์–ด ํ”„๋ ˆ์ž„ / ๋งˆ์ดํŽ˜์ด์ง€ ๊ตฌํ˜„

EarthSea 2024. 4. 24. 02:32

 

 

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

์˜ค๋Š˜์€ ์ƒˆ๋กœ์šด ํŒ€ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•œ ๋‚ ์ž…๋‹ˆ๋‹ค!

ํŒ€์›๋“ค๊ณผ ์ƒ์˜ ๋์— ํผ์Šค๋„ ๋ชจ๋นŒ๋ฆฌํ‹ฐ์•ฑ์„ ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ์–ด์š”~!

MapKit์„ ์ฒ˜์Œ ์‚ฌ์šฉํ•ด ๋ด์„œ ๊ผญ ๋„์ „ํ•ด๋ณด๊ณ  ์‹ถ์—ˆ๋˜ ์•ฑ์ด์—ˆ์Šต๋‹ˆ๋‹ค!

 

 

 

์ดˆ๊ธฐ ์™€์ด์–ด ํ”„๋ ˆ์ž„

 

์ดˆ๊ธฐ์˜ ์™€์ด์–ด ํ”„๋ ˆ์ž„์ž…๋‹ˆ๋‹ค. ์ €ํฌ๋Š” GCOO ์•ฑ์„ ๋ชจ๋ฐฉํ•˜์—ฌ ์•ฑ์„ ๋””์ž์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค~!

 

์ด 5๊ฐœ์˜ ํŽ˜์ด์ง€๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋กœ๊ทธ์ธ ํ™”๋ฉด
  • ํšŒ์›๊ฐ€์ž… ํ™”๋ฉด
  • ๋งต ํ™”๋ฉด
  • ์‚ฌ์šฉ์ž ํ™”๋ฉด
  • ๋Œ€์—ฌ ํ™”๋ฉด

 

 

 

์ด๋ฒˆ์— ์ œ๊ฐ€ ๋งก์€ ๊ตฌํ˜„ ๋ถ€๋ถ„์€ ์‚ฌ์šฉ์ž ํ™”๋ฉด์ž…๋‹ˆ๋‹ค!โœจ

์‚ฌ์šฉ์ž ํ™”๋ฉด ( ๋งˆ์ดํŽ˜์ด์ง€ ) ๊ตฌํ˜„

 

๊ตฌํ˜„ ์ „ ๋กœ์ง ๊ตฌ์ƒํ•˜๊ธฐ

 

์ œ๋ชฉ ๋ถ€๋ถ„์€ ๋’ค๋กœ ๊ฐ€๊ธฐ ๋ฒ„ํŠผ์ด ์ž๋™์œผ๋กœ ์ƒ์„ฑ์ด ๋˜๋Š” ๋‚ด๋น„๊ฒŒ์ด์…˜ ๋ฐ”๋ฅผ ์ด์šฉํ•˜์—ฌ,

ํ”„๋กœํ•„, ๋Œ€์—ฌ์ค‘, ์ด์ „ ๋Œ€์—ฌ ๋ชฉ๋ก ๋ถ€๋ถ„์€ ํ…Œ์ด๋ธ” ๋ทฐ์˜ ์„ธ์…˜์„ ๋‚˜๋ˆ„์–ด์„œ ๊ตฌํ˜„์„ ํ•ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์–ด์š”~!

 

 

 

? View๋กœ ๊ตฌํ˜„ํ•ด๋„ ๋˜๋Š”๋ฐ ์™œ ๊ตณ์ด ํ…Œ์ด๋ธ” ๋ทฐ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‚˜

์‚ฌ์šฉ์ž๊ฐ€ ํ‚ฅ๋ณด๋“œ๋ฅผ ๋Œ€์—ฌ์ค‘์ผ ๋•Œ๋Š” ๋Œ€์—ฌ์ค‘ ๋ถ€๋ถ„์ด ์‚ฌ์šฉ์ž ์ •๋ณด ๋ถ€๋ถ„์—์„œ ๋ณด์—ฌ์•ผ ํ•˜๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ ํ‚ฅ๋ณด๋“œ๋ฅผ ๋Œ€์—ฌ์ค‘์ด์ง€ ์•Š์„ ๋•Œ๋Š” ๋Œ€์—ฌ์ค‘ ๋ถ€๋ถ„์ด ๋ณด์ด์ง€ ์•Š์•„ ์ด์ „ ๋Œ€์—ฌ ๋ชฉ๋ก ๋ถ€๋ถ„์ด ํ”„๋กœํ•„ ๋ฐ”๋กœ ์•„๋ž˜๋กœ ์˜ค๊ฒŒ๋” ์˜คํ† ๋ ˆ์ด์•„์›ƒ์„ ์กฐ์ •ํ•ด์•ผ ํ–ˆ๋Š”๋ฐ, ๊ทธ ๋ถ€๋ถ„์—์„œ ์„ธ์…˜์„ ๋‚˜๋ˆ„์–ด์„œ ์„ธ์…˜์„ ์—†์• ๋Š” ๋ฐฉ๋ฒ•์ด ๊ตฌํ˜„ํ•˜๊ธฐ์— ํŽธํ•  ๊ฒƒ ๊ฐ™์•˜์–ด์š”!

 

๋˜ํ•œ "๋Œ€์—ฌ์ค‘"์˜ ๋‚ด์šฉ ๋ถ€๋ถ„๊ณผ "์ด์ „ ๋Œ€์—ฌ ๋ชฉ๋ก"์˜ ๋‚ด์šฉ ๋ถ€๋ถ„์˜ ํ˜•ํƒœ๊ฐ€ ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ๋ฐ˜๋ณต์ ์ธ View๋ฅผ ๊ทธ๋ฆฌ๋Š” ๋Œ€์‹  ๊ฐ™์€ cell๋กœ ๊ตฌํ˜„์„ ํ•˜๋˜ ๋‚ด์šฉ์„ ๋ฐ”๊พธ๋Š” ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์„ ๋– ์˜ฌ๋ ธ์Šต๋‹ˆ๋‹ค. ์ด์ „ ๋Œ€์—ฌ ๋ชฉ๋ก ๋ถ€๋ถ„์€ ์›๋ž˜ TableView๋กœ ๊ทธ๋ ค์•ผ ํ•˜๋‹ˆ ํ•ด๋‹น cell์„ ๋Œ€์—ฌ์ค‘์—์„œ ์žฌ์‚ฌ์šฉํ•˜๊ณ ์ž ํ–ˆ์–ด์š”!

 

"ํ”„๋กœํ•„", "๋Œ€์—ฌ์ค‘", "์ด์ „ ๋Œ€์—ฌ ๋ชฉ๋ก" ๋ผ๋ฒจ๋„ ๋™์ผํ•œ ๊ธ€๊ผด๊ณผ ๊ธ€๊ผด ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด Section์˜ ์ œ๋ชฉ์œผ๋กœ ๋“ค์–ด๊ฐ„๋‹ค๋ฉด ์ฝ”๋“œ ์ค‘๋ณต์„ ๋ฐฉ์ง€ํ•  ์ˆ˜๋„ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค!

 

์ตœ์ข…์ ์œผ๋กœ ์ „์ฒด 3๊ฐœ์˜ section์œผ๋กœ ๋‚˜๋ˆ„์–ด ํ”„๋กœํ•„ ๋ถ€๋ถ„์˜ cell๊ณผ ๋Œ€์—ฌ์ค‘/์ด์ „ ๋Œ€์—ฌ ๋ชฉ๋ก์˜ cell์„ xib๋กœ ๊ตฌํ˜„ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ์–ด์š”!

 

 

 

UI ์ž‘์—…ํ•˜๊ธฐ

 

์šฐ์„  TableView์™€ ๋กœ๊ทธ์•„์›ƒ ๋ฒ„ํŠผ์˜ UI๋ฅผ ์žก์•„์ฃผ์—ˆ์–ด์š”!

 

cell๋“ค๋„ ๋”ฐ๋กœ xib ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ ๊ตฌํ˜„ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค!

 

 

 

UI ์—ฐ๊ฒฐํ•˜๊ธฐ

๊ฐ๊ฐ์˜ UI์™€ ViewController ๊ฐ„์˜ object๋“ค์„ ์—ฐ๊ฒฐํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

UserViewController.swift

@IBOutlet weak var userTableView: UITableView!
@IBOutlet weak var logoutButton: UIButton!

 

UserDefaultTableViewCell.swift

@IBOutlet weak var firstLabel: UILabel!
@IBOutlet weak var LastLabel: UILabel!

 

RentalListTableViewCell.swift

@IBOutlet weak var rentalStartTimeLabel: UILabel!
@IBOutlet weak var kickBoardIDLabel: UILabel!
@IBOutlet weak var kickBoardNumberLabel: UILabel!
@IBOutlet weak var rentalTimeLabel: UILabel!
@IBOutlet weak var rentalPriceLabel: UILabel!

 

 

 

TableView ๊ตฌํ˜„ํ•˜๊ธฐ

 

ViewController์˜ ์ฝ”๋“œ์™€ TableView์˜ ์ฝ”๋“œ๋ฅผ ๋ถ„๋ฆฌ์‹œ์ผœ ์ฃผ๊ธฐ ์œ„ํ•ด์„œ ๋”ฐ๋กœ swift ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

์šฐ์„  tableView๋ฅผ ์„ธํŒ…ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ธฐ๋ณธ์ ์ธ delegate์™€ dataSource๋ฅผ ์„ค์ •ํ•ด ์ฃผ์—ˆ๊ณ ์š”!

func setUserTableView() {
    
    userTableView.delegate = self
    userTableView.dataSource = self
    
    let nib1 = UINib(nibName: "UserDefaultTableViewCell", bundle: nil)
    let nib2 = UINib(nibName: "RentalListTableViewCell", bundle: nil)
    
    userTableView.register(nib1, forCellReuseIdentifier: "userDefaultCell")
    userTableView.register(nib2, forCellReuseIdentifier: "rentalListCell")
}

๋‘ ๊ฐ€์ง€์˜ cell XIB ํŒŒ์ผ์„ ์—ฐ๊ฒฐํ•ด ์ฃผ๋Š” ์ฝ”๋“œ๋„ ๋„ฃ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

cell์—์„œ identifier ์„ค์ •ํ•˜๋Š” ๊ฒƒ์„ ๊นŒ๋จน์œผ์‹œ๋ฉด ์•ˆ ๋ผ์š”~!!!

 

 

 

TableView์˜ Section ์ˆ˜ ๊ฒฐ์ •

// MARK: - ํ…Œ์ด๋ธ” ๋ทฐ์˜ Section ์ˆ˜ ๊ฒฐ์ •
func numberOfSections(in tableView: UITableView) -> Int {
    
    // ๋Œ€์—ฌ์ค‘์ด๋ฉด section ์ˆ˜ 3๊ฐœ / ๋Œ€์—ฌ์ค‘์ด ์•„๋‹ˆ๋ผ๋ฉด section ์ˆ˜ 2๊ฐœ
    if userData.status {
        return 3
    }else {
        return 2
    }
}

์œ ์ €๊ฐ€ ํ‚ฅ๋ณด๋“œ๋ฅผ ๋Œ€์—ฌ์ค‘์ด๋ผ๋ฉด 3๊ฐœ์˜ Section์„ ๋Œ€์—ฌ์ค‘์ด ์•„๋‹ˆ๋ผ๋ฉด 2๊ฐœ์˜ Section์„ ์ถœ๋ ฅํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜์˜€์–ด์š”!

 

 

 

TableView์˜ SectionHeader ์„ค์ •

func tableView(_ tableView: UITableView,
               viewForHeaderInSection section: Int) -> UIView? {
    
    let headerView = UIView()
    headerView.backgroundColor = .white
    
    // headerLabel ์„ค์ •
    let headerLabel = UILabel(frame: CGRect(x: 20, y: 0, width: tableView.bounds.size.width, height: 45))
    headerLabel.font = UIFont.systemFont(ofSize: 16, weight: .bold)
    headerLabel.textColor = UIColor.black // ํ…์ŠคํŠธ ์ƒ‰์ƒ ์„ค์ •
    
    // hearerBorderView ์„ค์ •
    let borderWidth: CGFloat = tableView.bounds.size.width - 40
	let borderX: CGFloat = (tableView.bounds.size.width - borderWidth) / 2
	let headerBorderView = UIView(frame: CGRect(x: borderX, y: 40, width: borderWidth, height: 2))
	headerBorderView.backgroundColor = .black

    // ๋Œ€์—ฌ์ค‘์ด๋ฉด section ์ˆ˜ 3๊ฐœ / ๋Œ€์—ฌ์ค‘์ด ์•„๋‹ˆ๋ผ๋ฉด section ์ˆ˜ 2๊ฐœ
    if userData.status {
        switch section {
        case 0: headerLabel.text = "ํ”„๋กœํ•„"
        case 1: headerLabel.text = "๋Œ€์—ฌ์ค‘"
        case 2: headerLabel.text = "์ด์ „ ๋Œ€์—ฌ ๋ชฉ๋ก"
        default: headerLabel.text = ""
        }
    }else {
        switch section {
        case 0: headerLabel.text = "ํ”„๋กœํ•„"
        case 1: headerLabel.text = "์ด์ „ ๋Œ€์—ฌ ๋ชฉ๋ก"
        default: headerLabel.text = ""
        }
    }
    
    headerView.addSubview(headerLabel)
    
    return headerView
}

tableview์˜ header ๋ถ€๋ถ„์˜ Lable์„ ์„ค์ •ํ•˜๊ณ  ๊ทธ ์•„๋ž˜ border๋ฅผ ์ถ”๊ฐ€ํ•œ ๋’ค section์— ๋”ฐ๋ฅธ header์˜ ์ด๋ฆ„์„ ์„ค์ •ํ•ด ์ฃผ์—ˆ์–ด์š”!

 

 

 

TableView์˜ Section์— ๋”ฐ๋ฅธ cell์˜ ๊ฐœ์ˆ˜

// MARK: - tableView์˜ ์…€์˜ ๊ฐœ์ˆ˜
func tableView(_ tableView: UITableView,
               numberOfRowsInSection section: Int) -> Int {
    
    // ๋Œ€์—ฌ์ค‘์ด๋ฉด section ์ˆ˜ 3๊ฐœ / ๋Œ€์—ฌ์ค‘์ด ์•„๋‹ˆ๋ผ๋ฉด section ์ˆ˜ 2๊ฐœ
    if userData.status {
        switch section {
        case 0: 2
        case 1: 1
        case 2: userData.rentalList.count
        default: 0
        }
    }else {
        switch section {
        case 0: 2
        case 1: userData.rentalList.count
        default: 0
        }
    }
}

์‚ฌ์šฉ์ž์˜ ๋Œ€์—ฌ์ค‘ ์—ฌ๋ถ€์— ๋”ฐ๋ฅธ section ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์ง€๋ฏ€๋กœ ๊ฐ๊ฐ์˜ section์ˆ˜์˜ cell ๊ฐœ์ˆ˜๋„ ๋”ฐ๋กœ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค!

Section 0 ๋ถ€๋ถ„์€ ์‚ฌ์šฉ์ž์˜ ์ด๋ฆ„๊ณผ ์•„์ด๋””๋งŒ ๋ณด์ผ ์˜ˆ์ •์ด๋ผ 2๋กœ ๊ณ ์ •ํ•˜์˜€๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ ๋Œ€์—ฌ์ค‘์ผ ๋•Œ์˜ Section 1 ๋ถ€๋ถ„์€ ์‚ฌ์šฉ์ž๊ฐ€ ํ˜„์žฌ ํƒ€๊ณ  ์žˆ๋Š” ํ‚ฅ๋ณด๋“œ ์ •๋ณด๋งŒ์„ ์ถœ๋ ฅํ•˜๊ณ , ์‚ฌ์šฉ์ž๋Š” ํ•œ ๋ฒˆ์— 1๊ฐœ์˜ ํ‚ฅ๋ณด๋“œ๋งŒ ํƒˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ 1๋กœ ๊ณ ์ •ํ•ด ๋‘์—ˆ์Šต๋‹ˆ๋‹ค~!

 

 

 

TableView์˜ section์— ๋”ฐ๋ฅธ ๋‹ค๋ฅธ Cell ์ถœ๋ ฅ

// MARK: - tableView์˜ cell ์ถœ๋ ฅ

func tableView(_ tableView: UITableView,
               cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    // ๋Œ€์—ฌ์ค‘์ด๋ฉด section ์ˆ˜ 3๊ฐœ / ๋Œ€์—ฌ์ค‘์ด ์•„๋‹ˆ๋ผ๋ฉด section ์ˆ˜ 2๊ฐœ
    if userData.status {
        switch indexPath.section {
        case 0:
            return setUserDefaultCell(tableView, indexPath: indexPath)
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: "rentalListCell", for: indexPath) as! RentalListTableViewCell
            cell.kickBoardIDLabel.text = currentRental.kickBoardID
            cell.kickBoardNumberLabel.text = "\(currentRental.kickBoardNumber)"
            cell.rentalPriceLabel.text = setPriceLabel(price: currentRental.rentalPrice)
            cell.rentalStartTimeLabel.text = currentRental.rentalStartTime
            if let rentalTotalTime = currentRental.rentalTotalTime {
                cell.rentalTimeLabel.text = "\(rentalTotalTime)๋ถ„"
            }
            return cell
        case 2:
            return setUserRentalListCell(tableView, indexPath: indexPath)
        default:
            fatalError("Unknown section")
        }
    }else {
        switch indexPath.section {
        case 0:
            return setUserDefaultCell(tableView, indexPath: indexPath)
        case 1:
            return setUserRentalListCell(tableView, indexPath: indexPath)
        default:
            fatalError("Unknown section")
        }
    }
}

 

์‚ฌ์šฉ์ž์˜ ํ‚ฅ๋ณด๋“œ ์‚ฌ์šฉ ์ค‘์— ๋”ฐ๋ฅธ section ๋ณ€๊ฒฝ๊ณผ ํ•ด๋‹น section์˜ cell์„ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.

์‚ฌ์šฉ์ค‘๊ณผ ์‚ฌ์šฉ ์ค‘์ด ์•„๋‹ ๋•Œ ๋ชจ๋‘ "ํ”„๋กœํ•„"์˜ ๋ถ€๋ถ„๊ณผ "์ด์ „ ๋Œ€์—ฌ ๋ชฉ๋ก"์˜ ๋ถ€๋ถ„์ด ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋ฐ˜๋ณตํ•ด์•ผ ๋˜์–ด์„œ ์ฝ”๋“œ์˜ ์ค‘๋ณต ๋ฐฉ์ง€๋ฅผ ์œ„ํ•ด ๋”ฐ๋กœ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

func setUserDefaultCell(_ tableView: UITableView,
                        indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "userDefaultCell", for: indexPath) as! UserDefaultTableViewCell
    let cellConfigure: [(String,String)] = [("์ด๋ฆ„", userData.userName), ("์•„์ด๋””", userData.id)]
    cell.firstLabel.text = cellConfigure[indexPath.row].0
    cell.LastLabel.text = cellConfigure[indexPath.row].1
    return cell
}


func setUserRentalListCell(_ tableView: UITableView,
                           indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "rentalListCell", for: indexPath) as! RentalListTableViewCell
    cell.kickBoardIDLabel.text = userData.rentalList[indexPath.row].kickBoardID
    cell.kickBoardNumberLabel.text = "\(userData.rentalList[indexPath.row].kickBoardNumber)"
    cell.rentalPriceLabel.text = setPriceLabel(price: userData.rentalList[indexPath.row].rentalPrice)
    cell.rentalStartTimeLabel.text = userData.rentalList[indexPath.row].rentalStartTime
    if let rentalTotalTime = userData.rentalList[indexPath.row].rentalTotalTime {
        cell.rentalTimeLabel.text = "\(rentalTotalTime)๋ถ„"
    }
    return cell
}

 

 

 

TableView์˜ Section Header์™€ Footer์˜ ๋†’์ด ์„ค์ •

// ์„น์…˜ ํ—ค๋”์˜ ๋†’์ด๋ฅผ ์„ค์ •
func tableView(_ tableView: UITableView,
               heightForHeaderInSection section: Int) -> CGFloat {
    return 45
}


// ์„น์…˜ ํ‘ธํ„ฐ์˜ ๋†’์ด๋ฅผ ์„ค์ •
func tableView(_ tableView: UITableView,
               heightForFooterInSection section: Int) -> CGFloat {
    return 5
}

ํ—ค๋”์™€ ํ‘ธํ„ฐ์˜ ๋†’์ด ์„ค์ • ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ํ—ค๋” ๋ถ€๋ถ„์˜ Section ๋†’์ด๋ฅผ ์กฐ์ ˆํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

 

 

์ฒœ ๋‹จ์œ„, ์ฐ๊ธฐ

func setPriceLabel(price: Int) -> String {
    let numberFormatter = NumberFormatter()
    numberFormatter.numberStyle = .decimal
    guard let priceString = numberFormatter.string(from: NSNumber(value: price)) else { return "" }
    return priceString + "์›"
}

NumberFormatter๋ฅผ ์ด์šฉํ•œ ์ฒœ ๋‹จ์œ„์˜ , ์ฐ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

 

 

๋ฌธ์ œ์  ( cell์˜ separator ์„ ์˜ ์•ž๋’ค ๊ฐ„๊ฒฉ์˜ ๋‹ค๋ฆ„ )

์˜ค๋Š˜๋„ ์–ด๊น€์—†์ด ๋“ฑ์žฅํ•˜๋Š” ๋ฌธ์ œ์ ์€ UI์ ์œผ๋กœ cell๊ณผ cell ์‚ฌ์ด์˜ ์„ ์ธ separator ์„ ์ด ๋งˆ์Œ์— ๋“ค์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

 

 

cell ์‚ฌ์ด์˜ ์„ ์ด ์œ„์˜ ์‚ฌ์ง„์ฒ˜๋Ÿผ ์•ž๋’ค์˜ ์ผ์ •ํ•œ ๊ฐ„๊ฒฉ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์•„๋ž˜์˜ ์‚ฌ์ง„์ฒ˜๋Ÿผ ์•ž์˜ ๋ถ€๋ถ„์˜ ๊ฐ„๊ฒฉ๋งŒ ๋„์–ด์ ธ ์žˆ์–ด์„œ ์‹ ๊ฒฝ์ด ์“ฐ์ด๋”๋ผ๊ณ ์š”~!

 

 

์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ View๋กœ ์ง์ ‘ ๊ฐ„๊ฒฉ ์„ ์„ ๊ทธ๋ฆฌ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์šฐ์„  cell์˜ ์„ ์„ ์—†์• ๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค.

userTableView.separatorStyle = .none

์ €๋Š” ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์ฒ˜์Œ tableView๋ฅผ ์„ธํŒ…ํ•˜๋Š” ํ•จ์ˆ˜์— ๋„ฃ์–ด์ฃผ์—ˆ์–ด์š”!

 

 

Cell์˜ ํ•˜๋‹จ์— UIView๋ฅผ ์ถ”๊ฐ€ํ•ด ์›ํ•˜๋Š” ๋ชจ์–‘์˜ ์„ ์„ ๊ทธ๋ ค์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

 

 

๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•

tableView์˜ separator์˜ ์—ฌ๋ฐฑ ์„ค์ •์„ ํ†ตํ•ด์„œ ์ฝ”๋“œ๋กœ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

tableView.separatorInsetReference = .fromCellEdges

 

 

[ SeparatorInsetReference ]

  • Automatic (default) : ์™ผ์ชฝ์— 15 ํฌ์ธํŠธ์˜ ๊ฐ„๊ฒฉ์ด ๋–จ์–ด์ง
  • Custom : ์™ผ์ชฝ์ด๋‚˜ ์˜ค๋ฅธ์ชฝ์— ์›ํ•˜๋Š” ๊ฐ„๊ฒฉ์„ ์„ค์ •
    • fromCellEdges : ์–‘ ๋์— ์—ฌ๋ฐฑ์„ ์คŒ
    • fromAutomaticInsets : ์‚ฌ์šฉ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ์„ค์ •ํ•ด ์คŒ. ( ๊ฒน์นจ ๋ฐฉ์ง€ )

 

์ถ”๊ฐ€๋กœ separator์˜ ์ƒ‰์ด๋‚˜ ์Šคํƒ€์ผ๋„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

[ separator ์ƒ‰ ์„ค์ • ]

tableView.separatorColor = .black

 

 

[ separator ์Šคํƒ€์ผ ์„ค์ • ]

tableView.separatorStyle = .singleLine
  • none : ์„ ์„ ํ‘œ์‹œํ•˜์ง€ ์•Š์Œ
  • singleLine(default) : ์„ ์„ ํ•˜๋‚˜๋งŒ ํ‘œ์‹œ
  • singleLineEtched : ์ง€๊ธˆ์€ ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ.

 

 

 

์ „์ฒด ์ฝ”๋“œ

import Foundation
import UIKit

extension UserViewController: UITableViewDelegate, UITableViewDataSource {
    
    
    func setUserTableView() {
        
        userTableView.delegate = self
        userTableView.dataSource = self
        userTableView.separatorStyle = .none
        
        let nib1 = UINib(nibName: "UserDefaultTableViewCell", bundle: nil)
        let nib2 = UINib(nibName: "RentalListTableViewCell", bundle: nil)
        userTableView.register(nib1, forCellReuseIdentifier: "userDefaultCell")
        userTableView.register(nib2, forCellReuseIdentifier: "rentalListCell")
    }
    
    
    func setUserDefaultCell(_ tableView: UITableView, 
                            indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "userDefaultCell", for: indexPath) as! UserDefaultTableViewCell
        let cellConfigure: [(String,String)] = [("์ด๋ฆ„", userData.userName), ("์•„์ด๋””", userData.id)]
        cell.firstLabel.text = cellConfigure[indexPath.row].0
        cell.LastLabel.text = cellConfigure[indexPath.row].1
        return cell
    }
    
    
    func setUserRentalListCell(_ tableView: UITableView,
                               indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "rentalListCell", for: indexPath) as! RentalListTableViewCell
        cell.kickBoardIDLabel.text = userData.rentalList[indexPath.row].kickBoardID
        cell.kickBoardNumberLabel.text = "\(userData.rentalList[indexPath.row].kickBoardNumber)"
        cell.rentalPriceLabel.text = setPriceLabel(price: userData.rentalList[indexPath.row].rentalPrice)
        cell.rentalStartTimeLabel.text = userData.rentalList[indexPath.row].rentalStartTime
        if let rentalTotalTime = userData.rentalList[indexPath.row].rentalTotalTime {
            cell.rentalTimeLabel.text = "\(rentalTotalTime)๋ถ„"
        }
        return cell
    }
    
    
    func setPriceLabel(price: Int) -> String {
        let numberFormatter = NumberFormatter()
        numberFormatter.numberStyle = .decimal
        guard let priceString = numberFormatter.string(from: NSNumber(value: price)) else { return "" }
        return priceString + "์›"
    }
    
    
    // MARK: - ํ…Œ์ด๋ธ” ๋ทฐ์˜ Section ์ˆ˜ ๊ฒฐ์ •
    func numberOfSections(in tableView: UITableView) -> Int {
        
        // ๋Œ€์—ฌ์ค‘์ด๋ฉด section ์ˆ˜ 3๊ฐœ / ๋Œ€์—ฌ์ค‘์ด ์•„๋‹ˆ๋ผ๋ฉด section ์ˆ˜ 2๊ฐœ
        if userData.status {
            return 3
        }else {
            return 2
        }
    }
    
    
    func tableView(_ tableView: UITableView, 
                   heightForHeaderInSection section: Int) -> CGFloat {
        return 45
    }

    
    // ์„น์…˜ ํ‘ธํ„ฐ์˜ ๋†’์ด๋ฅผ ์„ค์ •
    func tableView(_ tableView: UITableView, 
                   heightForFooterInSection section: Int) -> CGFloat {
        return 5
    }

    
    func tableView(_ tableView: UITableView,
                   viewForHeaderInSection section: Int) -> UIView? {
        
        let headerView = UIView()
        headerView.backgroundColor = .white
        
        let headerLabel = UILabel(frame: CGRect(x: 20, y: 0, width: tableView.bounds.size.width, height: 45))
        headerLabel.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        headerLabel.textColor = UIColor.black // ํ…์ŠคํŠธ ์ƒ‰์ƒ ์„ค์ •
        
        let borderWidth: CGFloat = tableView.bounds.size.width - 40
        let borderX: CGFloat = (tableView.bounds.size.width - borderWidth) / 2
        let headerBorderView = UIView(frame: CGRect(x: borderX, y: 40, width: borderWidth, height: 2))
        headerBorderView.backgroundColor = .black
        
        // ๋Œ€์—ฌ์ค‘์ด๋ฉด section ์ˆ˜ 3๊ฐœ / ๋Œ€์—ฌ์ค‘์ด ์•„๋‹ˆ๋ผ๋ฉด section ์ˆ˜ 2๊ฐœ
        if userData.status {
            switch section {
            case 0: headerLabel.text = "ํ”„๋กœํ•„"
            case 1: headerLabel.text = "๋Œ€์—ฌ์ค‘"
            case 2: headerLabel.text = "์ด์ „ ๋Œ€์—ฌ ๋ชฉ๋ก"
            default: headerLabel.text = ""
            }
        }else {
            switch section {
            case 0: headerLabel.text = "ํ”„๋กœํ•„"
            case 1: headerLabel.text = "์ด์ „ ๋Œ€์—ฌ ๋ชฉ๋ก"
            default: headerLabel.text = ""
            }
        }
        
        headerView.addSubview(headerLabel)
        headerView.addSubview(headerBorderView)
        
        return headerView
    }
    
    
    // MARK: - tableView์˜ ์…€์˜ ๊ฐœ์ˆ˜
    func tableView(_ tableView: UITableView,
                   numberOfRowsInSection section: Int) -> Int {
        
        // ๋Œ€์—ฌ์ค‘์ด๋ฉด section ์ˆ˜ 3๊ฐœ / ๋Œ€์—ฌ์ค‘์ด ์•„๋‹ˆ๋ผ๋ฉด section ์ˆ˜ 2๊ฐœ
        if userData.status {
            switch section {
            case 0: 2
            case 1: 1
            case 2: userData.rentalList.count
            default: 0
            }
        }else {
            switch section {
            case 0: 2
            case 1: userData.rentalList.count
            default: 0
            }
        }
    }
    
    
    // MARK: - tableView์˜ cell ์ถœ๋ ฅ
    
    func tableView(_ tableView: UITableView,
                   cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        // ๋Œ€์—ฌ์ค‘์ด๋ฉด section ์ˆ˜ 3๊ฐœ / ๋Œ€์—ฌ์ค‘์ด ์•„๋‹ˆ๋ผ๋ฉด section ์ˆ˜ 2๊ฐœ
        if userData.status {
            switch indexPath.section {
            case 0:
                return setUserDefaultCell(tableView, indexPath: indexPath)
            case 1:
                let cell = tableView.dequeueReusableCell(withIdentifier: "rentalListCell", for: indexPath) as! RentalListTableViewCell
                cell.kickBoardIDLabel.text = currentRental.kickBoardID
                cell.kickBoardNumberLabel.text = "\(currentRental.kickBoardNumber)"
                cell.rentalPriceLabel.text = setPriceLabel(price: currentRental.rentalPrice)
                cell.rentalStartTimeLabel.text = currentRental.rentalStartTime
                if let rentalTotalTime = currentRental.rentalTotalTime {
                    cell.rentalTimeLabel.text = "\(rentalTotalTime)๋ถ„"
                }
                return cell
            case 2:
                return setUserRentalListCell(tableView, indexPath: indexPath)
            default:
                fatalError("Unknown section")
            }
        }else {
            switch indexPath.section {
            case 0:
                return setUserDefaultCell(tableView, indexPath: indexPath)
            case 1:
                return setUserRentalListCell(tableView, indexPath: indexPath)
            default:
                fatalError("Unknown section")
            }
        }
    }

    
}

 

 

 

์ ์šฉํ™”๋ฉด

์‚ฌ์šฉ์ž๊ฐ€ ํ‚ฅ๋ณด๋“œ๋ฅผ ๋Œ€์—ฌ์ค‘์ผ ๊ฒฝ์šฐ
์‚ฌ์šฉ์ž๊ฐ€ ํ‚ฅ๋ณด๋“œ๋ฅผ ๋Œ€์—ฌ์ค‘์ด ์•„๋‹ ๊ฒฝ์šฐ