ํฐ์คํ ๋ฆฌ ๋ทฐ
์ํฉ์ ์ด๋ ์ต๋๋ค
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ํ์ฌ ํด๊ฒฐํ ์ ์์ต๋๋ค
๊ฒฐ๋ก
>>>>>>>
data.count ๋ณ๊ฒฝ์ฌํญ์ด ์๋ค๋ฉด reloadData() ํธ์ถ
๋ณ๊ฒฝ์ฌํญ์ด ์๋ค๋ฉด reloadRow()ํธ์ถ
์๋๋ ์ ์ฒด ์์ ์ฝ๋์ ๋๋ค
//
// 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)
}
- Total
- Today
- Yesterday
- swift excel read
- swift urlsession network module
- readysay
- ๊ณต๋ถ ํ์ด๋จธ ์ดํ
- swift get excel
- swift network refactoring
- focus timer ์ดํ
- swift ์์ ๊ฐ์ ธ์ค๊ธฐ
- swift ์๊ฐ
- llm pdf rag
- ์๋์ํํธ ๋ ์ด์ธ์ด
- swift queryitem encode
- swift filemanager get excel
- swift ์์ ์ฝ๊ธฐ
- ๋ ๋์ธ์ด ์ดํ
- swift filemanager excel
- chatgpt rag llm
- swift ๋คํธ์ํฌ ๋ชจ๋ํ
- rag llm pdf
- swift urlsession module
- swift urlsession refactoring
- rag ๊ธฐ๋ฐ llm ์ฑ๋ด
- filemanager excel read
- rag ๊ธฐ๋ฐ llm
- ๋ ๋์ธ์ด
- swift urlsession ๊ณตํตํ
- swift urlcomponent encode
- llm csv
- swift network ๊ณตํตํ
- swift network module
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |