UITableView Swift中带有布尔值的切换按钮

时间:2018-10-07 08:44:40

标签: swift core-data tableview toggle

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

3 个答案:

答案 0 :(得分:2)

嗯,我建立了一个简单的项目,并使用协议找出了一些东西, 首先,您定义一个这样的协议:

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

这是一个简单的项目,但是您可以了解如何执行此操作。另外,请注意协议的定义和用法,这样您就不会错过任何内容。 而输出是这个Out Put of The Project

答案 1 :(得分:0)

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

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

答案 2 :(得分:-1)

将委托方法添加到您的单元格- (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;)