포스트

프로젝트 1일차

프로젝트가 시작되었다.

내가 담당하는건 TableView이다.

CollectionView에서 이미지를 선택하는 이벤트가 발생했을때, 처리를 하는게 내가 할 역할이다.

코드는 아래와 같다.

중간중간 기능을 구현하면서 작성하지 않아서 코드내 주석으로 대신한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import UIKit

class ViewController: UIViewController {
    
    
    @IBOutlet weak var tableView: UITableView!
    
    @IBOutlet weak var countLabel: UILabel!
    
    @IBOutlet weak var priceLabel: UILabel!
    
    var selectedList: [AppleProduct] = [AppleProduct]()
    
    
    var totalCount = 0
    var totalPrice = 0
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.rowHeight = 75
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UINib(nibName: Constants.cellName, bundle: nil), forCellReuseIdentifier: Constants.cellName)
        
        tableView.reloadData()
    }
    
    func getData() { // 전체 값, 개수를 적용.
        
        totalCount = selectedList.map{$0.value}.reduce(0, +)
        totalPrice = selectedList.map{Int($0.value * $0.price)}.reduce(0, +)
        
        priceLabel.text = "\(String(totalPrice)) 원"
        countLabel.text = "\(String(totalCount)) 개"
    }
    
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return selectedList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let currentLocation = indexPath.row
        
        guard let cell = tableView.dequeueReusableCell(withIdentifier: Constants.cellName, for: indexPath) as? PriceCell else { return UITableViewCell()
        }
        
        cell.selectionStyle = .none // 선택 안되게
        cell.titleLabel.text = selectedList[currentLocation].name
        cell.itemImage.image = selectedList[currentLocation].image
        cell.priceLabel.text = String(selectedList[currentLocation].price)
        cell.valueLabel.text = String(selectedList[currentLocation].value)
        
        // Tag를 부여
        cell.minusBtn.tag = currentLocation
        cell.plusBtn.tag = currentLocation
        cell.deleteBtn.tag = currentLocation
        
        cell.minusBtn.addTarget(self, action: #selector(minusValue), for: .touchUpInside)
        cell.plusBtn.addTarget(self, action: #selector(plusValue), for: .touchUpInside)
        cell.deleteBtn.addTarget(self, action: #selector(deleteValue), for: .touchUpInside)
        
        return cell
    }
    
    @objc func minusValue(sender: UIButton) {
        var currentValue = selectedList[sender.tag].value
        if let cell = tableView.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as? PriceCell {
            if currentValue != 1 { // 1 아래로 떨어지면 안되므로
                currentValue -= 1
                selectedList[sender.tag].value = currentValue
                cell.valueLabel.text = String(currentValue)
                getData()
            } else {
                currentValue = 1 //1 을 유지
            }
        }
        
    }
    
    @objc func plusValue(sender: UIButton) {
        var currentValue = selectedList[sender.tag].value
        
        if let cell = tableView.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as? PriceCell {
            
            currentValue += 1
            selectedList[sender.tag].value = currentValue
            cell.valueLabel.text = String(currentValue)
            
            getData()
        }
    }
    
    @objc func deleteValue(sender: UIButton) {
    
        selectedList.remove(at: sender.tag)
        tableView.reloadData()
        getData()
        
    }
}

Apr-01-2024-18-59-57

기술 면접 후기

현재 내가 알고 있는 지식에 2뎁스 정도 더 생각해서 확장시켜서 생각해두면 좋다고 하셨다. 그리고 대답을할때 간결하게 하자.

간결하게 하려고하는데 머리속에있는걸 그대로 이야기 하다보니 길어진듯하다.

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.