ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

์ƒํ™ฉ์€ ์ด๋ ‡์Šต๋‹ˆ๋‹ค

 

 

1. TableView์˜ data๊ฐ€ ์ง€์†์ ์œผ๋กœ ๋ณ€ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

2. TableView์˜ ํŠน์ • index๋งŒ update ํ•ด์ฃผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.  (์‹ค์ œ๋กœ๋Š” ๋น„๋™๊ธฐ ์‹œ์ ์— updateํ•ด์ฃผ๋Š” ์ƒํ™ฉ)

 

 

 

์ œ๊ฐ€ ๊ฒช์€ ๋ฌธ์ œ๋Š”

 

@objc func buttonPressed() {
        // ๋ฒ„ํŠผ ๋ˆŒ๋ €์„๋•Œ data ๋ณ€๊ฒฝ ์ด์ „ ๋˜๋Š” ์ดํ›„ ํ…Œ์ด๋ธ”๋ทฐ ์—…๋ฐ์ดํŠธ ๋˜๋„๋ก
        DispatchQueue.main.async {
            self.tableView.reloadRows(at: [IndexPath(row: 3, section: 0)], with: .automatic)
        }
        self.dataArray = ["1","2","3","4","5","6"]
    }

 

 

self.tableView.reloadRows()

๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ์„ ํ•œ ์‹œ์ ์—์„œ

 

 

Assertion failure in -[UITableView _Bug_Detected_In_Client_Of_UITableView_Invalid_Number_Of_Rows_In_Section:], UITableView.m:2581

2024-07-24 12:55:02.638712+0900 -[935:156579] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (13) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). 

 

์ด๋Ÿฌํ•œ ์—๋Ÿฌ๊ฐ€ ๋œน๋‹ˆ๋‹ค

 

 

--------------------------------------------------------------------------------------------------

 

 

์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„ 

 

'self.tableView.reloadRows(at: [Idx], with: .automatic)' ๋ฅผ

ํ˜ธ์ถœํ•˜๊ธฐ ์ด์ „ ๋˜๋Š” ์ดํ›„ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ์•ˆ๋œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 

 

์ด๋Ÿฐ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ์ƒํ™ฉ์—์„œ ํŠน์ • ์…€๋งŒ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ์—๋Š”

 

 

 

*oldData๋Š” tableView reloadRows ํ•˜๋ ค๊ณ  ํ•œ์‹œ์  (๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์ „)์˜ data๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

if oldData.count == newData.count {
    self.tableView.reloadRows(at ~)
} else {
    self.tableView.reloadData()
}

 

์ด๋Ÿฐ์‹์œผ๋กœ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌํ•˜์—ฌ tableView์˜ ๋ฐ์ดํ„ฐ ์•ˆ์ „์„ฑ์„ ๋ณด์žฅํ•˜๋„๋ก ํ•˜์—ฌ,

์•ˆ์ „ํ•˜๊ฒŒ tableView๋ฅผ updateํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

 

 

์•„๋ž˜๋Š” ์ „์ฒด ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค

 

 

//
//  ViewController.swift
//  240725_TableViewAsyncReload02
//
//  Created by jh on 7/25/24.
//

import UIKit

class ViewController: UIViewController {
    var tableView: UITableView!
    private let button: UIButton = {
        var config = UIButton.Configuration.filled()
        let btn = UIButton(configuration: config)
        btn.setTitle("button", for: .normal)
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    }()
    var dataArray: [String] = ["1","2","3"]
    
    init() {
        super.init(nibName: nil, bundle: nil)
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setUp()
    }
}


extension ViewController {
    private func setUp() {
        configure()
        setTableView()
        addViews()
        setConstraints()
    }
    private func configure() {
        view.backgroundColor = .white
        button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
    }
    
    private func setTableView(){
        tableView = UITableView(frame: .zero, style: .insetGrouped)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.translatesAutoresizingMaskIntoConstraints = false
    }
    
    @objc func buttonPressed() {
        // ๋ฒ„ํŠผ ๋ˆŒ๋ €์„๋•Œ data ๋ณ€๊ฒฝ ์ด์ „ ๋˜๋Š” ์ดํ›„ ํ…Œ์ด๋ธ”๋ทฐ ์—…๋ฐ์ดํŠธ ๋˜๋„๋ก
        
        let oldDataCount = dataArray.count // data๋ณ€๊ฒฝ ์ด์ „ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜
        DispatchQueue.main.async {
            // 6 != 3 ์ด๋ฏ€๋กœ else๋กœ ๋น ์ง
            if self.dataArray.count == oldDataCount {
                self.dataArray[3] = "new data"
                self.tableView.reloadRows(at: [IndexPath(row: 3, section: 0)], with: .automatic)
            } else {
                self.tableView.reloadData()
            }
        }
        self.dataArray = ["1","2","3","4","5","6"]
    }
    
    private func addViews() {
        self.view.addSubview(button)
        self.view.addSubview(tableView)
    }
    
    private func setConstraints() {
        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: button.topAnchor, constant: -30).isActive = true
        button.widthAnchor.constraint(equalToConstant: 200).isActive = true
        button.heightAnchor.constraint(equalToConstant: 60).isActive = true
        button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        button.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true
    }
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = dataArray[indexPath.row]
        return cell
    }
}

 

 

 

 

 

 

// ์•„๋ž˜์ฒ˜๋Ÿผ ์ž‘์„ฑํ•  ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋œธ

 

@objc func buttonPressed() {
        // ๋ฒ„ํŠผ ๋ˆŒ๋ €์„๋•Œ data ๋ณ€๊ฒฝ ์ด์ „ ๋˜๋Š” ์ดํ›„ ํ…Œ์ด๋ธ”๋ทฐ ์—…๋ฐ์ดํŠธ ๋˜๋„๋ก
        self.dataArray = ["1","2","3","4","5","6"]
        DispatchQueue.main.async {
            self.tableView.reloadRows(at: [IndexPath(row: 3, section: 0)], with: .automatic)
        }
    }
@objc func buttonPressed() {
        // ๋ฒ„ํŠผ ๋ˆŒ๋ €์„๋•Œ data ๋ณ€๊ฒฝ ์ด์ „ ๋˜๋Š” ์ดํ›„ ํ…Œ์ด๋ธ”๋ทฐ ์—…๋ฐ์ดํŠธ ๋˜๋„๋ก
        DispatchQueue.main.async {
            self.tableView.reloadRows(at: [IndexPath(row: 3, section: 0)], with: .automatic)
        }
        self.dataArray = ["1","2","3","4","5","6"]
    }

 

 


------------------------------------------------------------------------------------------------------------

 

 

์ถ”๊ฐ€์ ์œผ๋กœ

DispatchQueue.main.async {
    self.tableView.reloadData()
    let count = self.dataArray.count
    self.tableView.scrollToRow(at: IndexPath(row: count - 1, section: 0), at: .bottom, animated: false)
}

 

์ด๋Ÿฌ๋ฉด ๋˜ ์œ„์˜ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

๊ทธ๋ž˜์„œ scrollToRow ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ• ๋•Œ reload ํ•˜๊ธฐ ์ „์˜ count๋ฅผ ์‚ฌ์šฉํ•ด์•ผ safety ํ•ฉ๋‹ˆ๋‹ค

DispatchQueue.main.async {
    let count = self.dataArray.count
    self.tableView.reloadData()
    self.tableView.scrollToRow(at: IndexPath(row: count - 1, section: 0), at: .bottom, animated: false)
}

 

 

 

 

๋Œ“๊ธ€