ios - 从 Tableview 的第 0 节中删除按钮

标签 ios swift xcode uitableview

我有一个包含 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 部分。

谁能想到一个更好的方法来只为第一部分删除此按钮?

Screenshot of section 0

Screenshot of section 1

最佳答案

第一次运行正确显示单元格,因为所有单元格都是单元格类的新实例(没有重用),但滚动后显示的单元格可能会被重用,这个重用的单元格可能是你删除的第零节中的单元格按钮,你可以尝试显示/隐藏它

 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/

相关文章:

ios - 使用 caanimation 改变层的高度

ios - 调整约束高度后,Swift 约束无法移回屏幕底部

Swift - UIStoryboardSegue 以编程方式弹出 ViewController

ios - 使用适用于 iOS 的 OpenCV 框架的 Apple Mach-O Linker (Id) 错误

ios - swift - 在没有虚拟间隔 View 的情况下垂直居中多个 View

ios - 使用 NavigationLink 打开新 View 时如何隐藏 TabView?

ios - Swift 线程安全计数器变量?用于跟踪何时删除网络事件指示器

ios - 在 iOS 中本地或远程安装 pod 时看不到额外添加的 .swift 文件,swift

swift - 将数据从 View Controller 传递到 tabbarcontroller

iphone - 如何在 Xcode 模板中包含静态库依赖项?