swift - 在 UITableView Swift 中使用 bool 值切换按钮

标签 swift core-data tableview toggle

我想添加一个 bool 切换以了解按钮是否已被按下(会将此值保存到核心数据(如果为真,还想将单元格数据保存到核心数据,如果为假,则从核心数据中删除))我我不知道该怎么做。任何帮助将不胜感激。如果需要来自 View Controller 的所有代码,请发表评论,我会这样做(我已经设置了 xcdatamodel)。

  @IBAction func buttonTapped(_ sender:UIButton) {
    var superView = sender.superview
    while !(superView is UITableViewCell) {
        superView = superView?.superview
    }
    let cell = superView as! UITableViewCell
    if let indexpath = tableView.indexPath(for: cell){

        print(indexpath)
    }
}

最佳答案

好吧,我构建了一个简单的项目,并使用协议(protocol)解决了一些问题, 首先你定义一个这样的协议(protocol):

protocol cellDidRequestSaving {
     func saveOrDelete(indexpath : Int)
}

首先在你的单元格中定义你这样的按钮:

class TableViewCell: UITableViewCell {
    var delegate: cellDidRequestSaving?
    var indexPath = 0 //come from the parent
    override func awakeFromNib() {
         super.awakeFromNib()

         // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
         super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    @IBAction func didTap(_ sender: Any) {
        // this protocol defined in the parent
        delegate?.saveOrDelete(indexpath: indexPath)

    }
}

现在在你的 tableViewController 中你使用这样的协议(protocol):

 class TableViewController: UITableViewController, cellDidRequestSaving {
     var cellStat = [Int:Bool]()
     override func viewDidLoad() {
         super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    // MARK: - Table view data source

     override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
         return 1
    }

     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
         return 3
    }

     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
         cell.indexPath = indexPath.row
         cell.delegate = self
        // Configure the cell...

         return cell
    }
     func saveOrDelete(indexpath: Int) {
         if let status = cellStat[indexpath], status {
            print(indexpath, "delete")
             cellStat[indexpath] = !status
        }
         else {
            print(indexpath, "save")
             cellStat[indexpath] = true
        }
    }

这是一个简单的项目,但您可以了解如何去做。另外,请注意协议(protocol)定义和用法,这样您就不会错过任何东西。 输出是这个 Out Put of The Project

关于swift - 在 UITableView Swift 中使用 bool 值切换按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52686867/

相关文章:

swift - 在 Swift 中重写 CIImage 最终会导致处理器下降到 0% 并卡住

swift - 为什么我无法获取位置?

swift - SwiftUI 中的多行可编辑文本字段

swift - 将数据迁移到应用程序组禁用 iCloud 同步

ios - NSUserDefault 在后退按钮上快速同步

ios - 将代码从 objective c 转换为 swift 时的常见函数问题

ios - 调试CoreData异常

ios - 如何在coredata中创建数据库 View

ios - 由于表格 View 中的可重用单元格,当 Youtube 播放器滚动到 View 之外时,它会丢失

ios - 无法在 Today Extension ios 中加载 TableView