我有一个包含 4 个部分的 TableView 。在第 2、3 和 4 节中,我想要一个 + 按钮来将信息添加到“已保存”数组。我有添加信息的逻辑设置,但我在使用 tableview 单元格时遇到问题。
我不希望 + 按钮出现在第 0 节中,因为那是我们添加数据的地方。这是我的 cellForRowAt 方法...
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! SchoolTableViewCell
// Configure the cell...
if indexPath.section == 0 {
cell.textLabel?.text = "Test"
cell.addFavoritesButton.removeFromSuperview()
} else if indexPath.section == 1 {
cell.textLabel?.text = Items.sharedInstance.elementaryList[indexPath.row]
} else if indexPath.section == 2 {
cell.textLabel?.text = Items.sharedInstance.intermediateList[indexPath.row]
} else if indexPath.section == 3 {
cell.textLabel?.text = Items.sharedInstance.highschoolList[indexPath.row]
}
return cell
一开始效果很好!但如果我向下滚动,越来越多的单元格将删除该按钮。由于可重复使用的单元格,它并没有将其限制在第 0 部分。
谁能想到一个更好的方法来只为第一部分删除此按钮?
最佳答案
第一次运行正确显示单元格,因为所有单元格都是单元格类的新实例(没有重用),但滚动后显示的单元格可能会被重用,这个重用的单元格可能是你删除的第零节中的单元格按钮,你可以尝试显示/隐藏它
if indexPath.section == 0 {
cell.textLabel?.text = "Test"
cell.addFavoritesButton.isHidden = true
}
else
{
cell.addFavoritesButton.isHidden = false
}
关于ios - 从 Tableview 的第 0 节中删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49434961/