我正在尝试在表视图底部添加一个按钮来处理 loadMore()
函数。
我试图做的是以编程方式在自定义单元格中定义一个按钮,并在 numberOfRows 优于 tableView 项目数时尝试附加它。这会导致致命错误“索引超出范围”。
这是我想要实现的目标:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemArray.count + 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let loadCell = itemList.dequeueReusableCell(withIdentifier: "loadMore", for: indexPath) as! LoadMore
let cell = itemList.dequeueReusableCell(withIdentifier: "normalCell", for: indexPath) as! ItemBox
if indexPath.row > itemArray.count {
return loadCell
}
return cell
}
我不明白为什么会出现此错误,因为我在 numberOfRowsInSection 函数中又指定了一行。