Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 在tableview中选择和取消选择行时发出执行函数_Swift_Uitableview - Fatal编程技术网

Swift 在tableview中选择和取消选择行时发出执行函数

Swift 在tableview中选择和取消选择行时发出执行函数,swift,uitableview,Swift,Uitableview,注意:TableView是使用JSON填充的,其结构包含codeNum值 我需要执行两个不同的函数,这取决于是否选择了行,以下是我现有的选择机制: class CheckableTableViewCell: UITableViewCell { override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier

注意:TableView是使用JSON填充的,其结构包含codeNum值

我需要执行两个不同的函数,这取决于是否选择了行,以下是我现有的选择机制:

class CheckableTableViewCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.accessoryType = selected ? .checkmark : .none
    }
}
选择一行时,需要运行
select()
,未选择的行时,需要运行
unselect()
。在此之前,需要将rows codeNum值指定给变量tappedSelected:

    structure = sections[indexPath.section].items
    let theStructure = structure[indexPath.row]
    tappedSelected = theStructure.codeNum

如何将其实现到自定义类中?

您应该尝试使用didSelectRowAt()和didSelectRowAt()方法。假设这里允许多个选择,我会这样做

如果选择了单元格 如果未选择单元格
在选择/取消选择单元格时,可以使用闭包调用方法

首先,在您的
CheckableTableViewCell
中创建一个
闭包
,并在
setSelected(uuu:animated:)
方法中使用
true/false
调用它,即

class CheckableTableViewCell: UITableViewCell {
    var handler: ((Bool)->())? //here....

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.accessoryType = selected ? .checkmark : .none
        handler?(selected) //here....
    }

    //rest of the code....
}
 class VC: UIViewController, UITableViewDataSource {
    //rest of the code...

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CheckableTableViewCell
        cell.handler = {[weak self](selected) in
            selected ? self?.select(indexPath) : self?.unselect()
        }
        return cell
    }

    func select(_ indexPath: IndexPath) {
        let structure = sections[indexPath.section].items
        let theStructure = structure[indexPath.row]
        tappedSelected = theStructure.codeNum
    }

    func unselect() {
        //add the code here...
    }
}
接下来,在您的
ViewController
中,为
tableView(uu:cellForRowAt:)
方法中的
CheckableTableViewCell
实例设置
handler
的值,即

class CheckableTableViewCell: UITableViewCell {
    var handler: ((Bool)->())? //here....

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.accessoryType = selected ? .checkmark : .none
        handler?(selected) //here....
    }

    //rest of the code....
}
 class VC: UIViewController, UITableViewDataSource {
    //rest of the code...

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CheckableTableViewCell
        cell.handler = {[weak self](selected) in
            selected ? self?.select(indexPath) : self?.unselect()
        }
        return cell
    }

    func select(_ indexPath: IndexPath) {
        let structure = sections[indexPath.section].items
        let theStructure = structure[indexPath.row]
        tappedSelected = theStructure.codeNum
    }

    func unselect() {
        //add the code here...
    }
}
这将解决在选中或取消选中单元格时调用
select()
unselect()
方法的问题


对于您的另一项要求,请说明您希望在什么时候将
codeNum
值设置为
tappedSelected
。而
tappedSelected
在哪里,在
ViewController
CheckableTableViewCell
中,哪里有select()和unselect()方法?在ViewController或TableViewCell中?tappedSelected是一个变量,一旦选定一行,该变量将包含codeNum的值。我在函数select()和unselect()中使用它。如果您有其他问题,请告诉我。在这种情况下,您可以编写与前面在
select()
方法中所做的相同的代码。select()和unselect()都是指“structure=sections…”吗?在测试代码时,我还注意到unselect()在TableViewController首次运行时继续执行