arrays - tableview 单元格添加说明第 0 节中的无效行数
问题描述
每次我点击添加按钮单元格。当用户点击添加按钮时,应立即将一个新单元格添加到 tableview 中,整个代码将进入运行时错误。我想修复该错误,以便您可以向 tableview 添加一个单元格。运行时错误状态
"无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (5) 必须等于更新前该节中包含的行数 (5),加上或减去从该部分插入或删除的行数(1 插入,0 删除)加上或减去移入或移出该部分的行数(0 移入,0 移出)。
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var addBTN = UIButton()
var arr = [1,1,3,3]
var currentIndex = 0
var arrayValues = [Int]()
var pic = UIImageView()
var addBtn = UIButton()
var cellCount = 5
var tableview = UITableView()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellCount // use a variable here so you can change it as you delete cells, else it will crash
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 118
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTv
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
[addBTN].forEach{
$0.translatesAutoresizingMaskIntoConstraints = false
view.addSubview($0)
}
tableview.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height * 0.8)
addBTN.frame = CGRect(x: view.frame.width / 2, y: view.frame.height * 0.8, width: view.frame.width / 2 , height: view.frame.height / 5)
addBTN.backgroundColor = .systemTeal
view.addSubview(tableview)
tableview.register(CustomTv.self, forCellReuseIdentifier: "cell")
tableview.delegate = self
tableview.dataSource = self
pic.isHidden = true
pic.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height )
addBTN.addTarget(self, action: #selector(newCell), for: .touchDown)
addBTN.setTitle("New cell", for: .normal)
}
@objc func newCell(){
arr.append(6) /// not sure what the numbers mean though...
tableview.beginUpdates()
tableview.insertRows(at: [IndexPath(row: arr.count - 1, section: 0)], with: .automatic) /// animate the insertion
tableview.endUpdates()
}
}
protocol CustomTvCellDelegateadd: AnyObject {
func addCeller(_ customTV: CustomTv)
}
class CustomTv: UITableViewCell { // it's good form to Pascal case your class name ;)
weak var delegate: CustomTvCellDelegate? // make that delegate a property of your cell
weak var delegatef: CustomTvCellDelegateadd?
lazy var backView : UIView = {
let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width , height: 110))
view.backgroundColor = .green
return view
}()
lazy var pic : UIImageView = {
let btn = UIImageView(frame: CGRect(x: 50-25, y: 6, width: 100 , height: 110))
btn.backgroundColor = .systemPink
return btn
}()
override func layoutSubviews() {
backView.clipsToBounds = true
backView.frame = CGRect(x: 0, y: 6, width: bounds.maxX , height: 110)
// moved these calls here instead of on setSelected(_selected:animated:)
addSubview(backView)
backView.addSubview(pic)
}
@objc func addButtonTapped() {
self.delegatef?.addCeller(self)
}
}
解决方案
那是因为您向名为 arr 的数组附加了一个值 (6),但是当 tableView 询问您返回的部分中的行数时cellCount
,您从不费心增加,因此它将继续返回 5。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellCount // use a variable here so you can change it as you delete cells, else it will crash
}
因此,当tableview.insertRows
执行时,tableView 期望行数增加数组中的元素数(在您的情况下,它期望行数为 6,但由于它的值不变, cellCount
它只会得到 5,因此 tableView 不知道并抛出错误提及
更新后现有节中包含的行数 (5) 必须等于更新前该节中包含的行数 (5),加上或减去从该节中插入或删除的行数(插入 1 , 0 已删除)并加上或减去移入或移出该部分的行数(0 移入,0 移出)。”
所以要么改变
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr.count
}
或使用
@objc func newCell(){
cellCount += 1 // update the value
arr.append(6)
tableview.beginUpdates()
tableview.insertRows(at: [IndexPath(row: arr.count - 1, section: 0)], with: .automatic) /// animate the insertion
tableview.endUpdates()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellCount
}
哪个对你更有意义:)
推荐阅读
- screen-scraping - PhpPhantom 请求返回“405 不允许”。有什么办法仍然可以抓取数据?
- python-3.x - 请求后重试机制问题
- php - CakePHP4:连接表上的连接位置未按预期工作
- javascript - 将纯文本转换为具有键值对的对象
- html - Typo3 中的 iframe 未加载
- java - Java 程序在从 main() 提取的变量之间输出空格,这很好。但是我想删除尾随空格
- c - 为什么即使变量没有改变 const 也不起作用?
- extjs - ExtJs 6 或 7 焦点树列表项进入视图
- email - 使用 Zoho Deluge 将 SMS 从 Twilio 转发到电子邮件
- python - 如何在另一个函数中使用一个函数中的值