【问题标题】:toggle button with boolean in UITableView SwiftUITableView Swift中带有布尔值的切换按钮
【发布时间】:2018-10-07 08:44:40
【问题描述】:

我想添加一个布尔切换来了解按钮是否已被按下(将此值保存到核心数据(如果为真,也想将单元格数据保存到核心数据,如果为假,则从核心数据中删除))我我不知道该怎么做。任何帮助将不胜感激。如果需要视图控制器中的所有代码,请留下评论,我会这样做(我已经设置了 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)
    }
}

【问题讨论】:

  • 你的按钮在哪里?是在牢房里吗?
  • @taratandel 是的,上面的代码是通过按下按钮来获取单元格的索引路径,我是否还必须在 xcdatamodel 中保存与单元格布尔值和数据相关的索引路径?跨度>
  • 如果我错了,请纠正我。您想按下单元格中的按钮并保存该单元格的数据吗?
  • @taratandel 是,如果再次按下按钮则删除,因此使用切换,默认为 false

标签: swift core-data tableview toggle


【解决方案1】:

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

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 使用这样的协议:

 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
        }
    }

这是一个简单的项目,但您可以了解如何做。另外,请注意协议定义和用法,这样您就不会错过任何内容。 输出是这个

【讨论】:

    【解决方案2】:

    向您的自定义单元格添加属性,例如:

    var indexPath: IndexPath = IndexPath(row: -1, section: -1)  //The -1 just know when it is not set yet
    

    在你的 tableView 中cellForRow

    cell.indexpath = indexPath
    

    假设 buttonTapped 在您的自定义单元格类中定义:

    @IBAction func buttonTapped(_ sender:UIButton) {
        print(indexpath)
    }
    

    【讨论】:

    • TableViewCell 不应该知道或关心它自己的 IndexPath,请向 tableview 询问该信息。
    【解决方案3】:

    为你的单元格添加一个委托方法- (void)myCell:(MyCell *)cell didTapButton:(UIButton *)button

    那么在您的 VC 中,您可以从单元格对象向后工作。

    - (void)myCell:(MyCell *)cell didTapButton:(UIButton *)button
    {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        MyObject *object = [self.dataSource objectAtIndexPath:indexPath];
    
        // Do something with the knowledge that the button in the cell for the object was tapped... 
    
        // For example
        object.tapped = YES;
        [object.managedObjectContext save:NULL];
    }
    

    只需要转换成 swift ;)

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 2017-09-27
      • 2012-02-25
      • 2021-12-24
      • 2019-04-19
      • 2018-05-16
      相关资源
      最近更新 更多