๐Ÿ“ฑ App Development Study/iOS๐ŸŽ

[Udemy iOS & Swift Bootcamp] ์ฑ„ํŒ… ์™„์„ฑํ•˜๊ธฐ 1 (๋ฐœ์‹ ์ž/์ˆ˜์‹ ์ž ๊ตฌ๋ถ„ํ•˜๊ธฐ)

ibelieveinme 2023. 7. 3. 21:57
728x90

์ง€๊ธˆ๊นŒ์ง€ ๋งŒ๋“  ์ฑ„ํŒ…์•ฑ์—์„œ ๋ถ€์กฑํ•œ 3๊ฐ€์ง€๋ฅผ ์ฑ„์›Œ๋ณด์ž.

 

1. ๋ฐœ์‹ ์ž/์ˆ˜์‹ ์ž ๊ตฌ๋ถ„ํ•˜๊ธฐ

2. ์ตœ์‹  ๋ฉ”์„ธ์ง€(๋งจ ์•„๋ž˜)์œผ๋กœ ํ™”๋ฉด ์Šคํฌ๋กคํ•˜๊ธฐ.

3. ์ƒ๋‹จ ๋„ค๋น„๊ฒŒ์ด์…˜ ๋ฐ” ๊พธ๋ฏธ๊ธฐ


1. ๋ฐœ์‹ ์ž/์ˆ˜์‹ ์ž ๊ตฌ๋ถ„ํ•˜๊ธฐ

1) ๊ธฐ์กด์— ๋งŒ๋“ค์—ˆ๋˜ Cell ์— another user ์ด๋ฏธ์ง€ ์ถ”๊ฐ€ํ•˜๊ธฐ.

์™ผ์ชฝ์˜ ๊ธฐ์กด Cell ์— right Image ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ์™ผ์ชฝ์— ๋ถ™์—ฌ๋„ฃ๊ณ  You ์ด๋ฏธ์ง€๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

์ฑ„ํŒ… ๋ฉ”์„ธ์ง€๊ฐ€ ๋ณธ์ธ์ด๋ฉด Me, ์ƒ๋Œ€๋ฐฉ์ด๋ฉด You ์ด๋ฏธ์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๊ฒŒ ํ•  ๊ฒƒ์ด๋‹ค.

 

2) MessageCell ์Šคํฌ๋ฆฝํŠธ์— leftImageView ๊ฐ์ฒด ์ถ”๊ฐ€ํ•˜๊ธฐ.

์ด๋ฏธ์ง€ ๊ฐ์ฒด๋ฅผ ์ƒํ™ฉ์— ๋งž๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋„๋ก ๊ฐ์ฒด๋กœ ์ถ”๊ฐ€ํ•ด์ฃผ์ž.

 

3) ChatViewController ํด๋ž˜์Šค์— ์ฝ”๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ

extension ChatViewController: UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return messages.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let message = messages[indexPath.row]
        
        let cell = tableView.dequeueReusableCell(withIdentifier: K.cellIdentifier, for: indexPath)
            as! MessageCell
        cell.label.text = messages[indexPath.row].body
        
        //This is a message from the current user.
        if message.sender == Auth.auth().currentUser?.email {
            cell.leftImageView.isHidden = true
            cell.rightImageView.isHidden = false
            cell.messageBubble.backgroundColor = UIColor(named: K.BrandColors.lightPurple)
            cell.label.textColor = UIColor(named: K.BrandColors.purple)
        }
        //This is a message from the another user.
        else {
            cell.leftImageView.isHidden = false
            cell.rightImageView.isHidden = true
            cell.messageBubble.backgroundColor = UIColor(named: K.BrandColors.purple)
            cell.label.textColor = UIColor(named: K.BrandColors.lightPurple)
        }
        return cell
    }
}

Auth.auth().currentUser?.email ๊ตฌ๋ฌธ์„ ์ด์šฉํ•ด์„œ ํ˜„์žฌ message sender ๊ฐ€ ๋ณธ์ธ์ด๋ฉด rightImage๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ๋งํ’์„  ์ƒ‰๊น”์„ ๋ฐ์€ ๋ณด๋ผ์ƒ‰, ๊ธ€์ž์ƒ‰์„ ๋ณด๋ผ์ƒ‰์œผ๋กœ ํ‘œํ˜„ํ•ด์ค€๋‹ค.

 

์ƒ๋Œ€๋ฐฉ์˜ ๋ฉ”์„ธ์ง€๋ผ๋ฉด leftImage๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ๋งํ’์„  ์ƒ‰๊น”์„ ๋ณด๋ผ์ƒ‰, ๊ธ€์ž์ƒ‰์„ ๋ณด๋ผ์ƒ‰์œผ๋กœ ํ‘œํ˜„ํ•ด์ค˜์„œ ๋ณธ์ธ๊ณผ ์ƒ๋Œ€๋ฐฉ์˜ ๋งํ’์„ ์„ ๊ตฌ๋ณ„ํ•ด์ค€๋‹ค.

 

4) ๊ฒฐ๊ณผํ™”๋ฉด

๊ณ„์ •์„ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค์–ด์„œ ์ ‘์†ํ•ด์„œ ์ฑ„ํŒ…์„ ๋ณด๋‚ด๋ณด๋ฉด ์ž˜ ๊ตฌ๋ถ„๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

728x90