Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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 如何识别哪个单元格被点击一次,哪个单元格被点击两次_Swift_Uitableview - Fatal编程技术网

Swift 如何识别哪个单元格被点击一次,哪个单元格被点击两次

Swift 如何识别哪个单元格被点击一次,哪个单元格被点击两次,swift,uitableview,Swift,Uitableview,那里!我想知道哪个单元格被点击一次,哪个单元格被点击两次。我有两个类,一个用于TableViewController,另一个用于TableViewCell。我想操纵有关触摸的细胞,但我无法获得它们的信息 TableViewController: import UIKit var elements: [[Int16]] = Array(repeating:Array(repeating:0, count:2), count:10) class ViewController: UIViewCon

那里!我想知道哪个单元格被点击一次,哪个单元格被点击两次。我有两个类,一个用于TableViewController,另一个用于TableViewCell。我想操纵有关触摸的细胞,但我无法获得它们的信息

TableViewController:

import UIKit

var elements: [[Int16]] = Array(repeating:Array(repeating:0, count:2), count:10)

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return elements.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

    if(elements[indexPath.row][1] == 1) //if red
    {
        cell.Label.text = String(elements[indexPath.row][0] * 3)
        cell.Circle.backgroundColor = UIColor.red
    }
    else //if blue
    {
        cell.Label.text = String(elements[indexPath.row][0])
        cell.Circle.backgroundColor = UIColor.blue
    }

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    return UIScreen.main.bounds.height/10
}

override func viewWillAppear(_ animated: Bool)
{
    for i in 0..<elements.count
    {
        elements[i][0] = Int16(Int(arc4random_uniform(10)))
        elements[i][1] = Int16(Int(arc4random_uniform(2)))
    }

    Memory().save(entity: elements)
}

override func viewDidLoad()
{
    super.viewDidLoad()
}

override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
}

}
在单元格内,我有一个标签,存储一个从0到10的随机数(标签),旁边有一个圆圈-蓝色或红色(它们在开始时也是随机的)。如果圆圈为红色,则数字(标签)显示乘以三的数字。所有这些都在那里

现在。。。我想通过触摸一次单元格来更改数字,并通过触摸两次将其设置为0

TableViewController:

import UIKit

var elements: [[Int16]] = Array(repeating:Array(repeating:0, count:2), count:10)

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return elements.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

    if(elements[indexPath.row][1] == 1) //if red
    {
        cell.Label.text = String(elements[indexPath.row][0] * 3)
        cell.Circle.backgroundColor = UIColor.red
    }
    else //if blue
    {
        cell.Label.text = String(elements[indexPath.row][0])
        cell.Circle.backgroundColor = UIColor.blue
    }

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    return UIScreen.main.bounds.height/10
}

override func viewWillAppear(_ animated: Bool)
{
    for i in 0..<elements.count
    {
        elements[i][0] = Int16(Int(arc4random_uniform(10)))
        elements[i][1] = Int16(Int(arc4random_uniform(2)))
    }

    Memory().save(entity: elements)
}

override func viewDidLoad()
{
    super.viewDidLoad()
}

override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
}

}
从这里引用

可能与ok重复,这样更好,但是现在如何获取被点击单元格的indexPath?您可以使用委托协议获取indexPath
var elements: [[Int16]] = Array(repeating: Array(repeating:0, count:2), count:10)

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    //MARK: - UIViewController LifeCycle
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        for i in 0..<elements.count {
            elements[i][0] = Int16(Int(arc4random_uniform(10)))
            elements[i][1] = Int16(Int(arc4random_uniform(2)))
        }
        Memory().save(entity: elements)
    }


    //MARK: - UITableView Delegate & DataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return elements.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

        if elements[indexPath.row][1] == 1 {
            cell.Label.text = String(elements[indexPath.row][0] * 3)
            cell.Circle.backgroundColor = UIColor.red
        }
        else {
            cell.Label.text = String(elements[indexPath.row][0])
            cell.Circle.backgroundColor = UIColor.blue
        }
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UIScreen.main.bounds.height/10
    }
}
class TableViewCell: UITableViewCell {

    @IBOutlet weak var Label: UILabel!
    @IBOutlet weak var Circle: UIView!
    private var tapCounter = 0

    override func awakeFromNib() {
        super.awakeFromNib()

        Circle.layer.cornerRadius = Circle.frame.width / 2

        let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction))
        addGestureRecognizer(tap)
    }

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

    @objc func tapAction() {

        if tapCounter == 0 {
            DispatchQueue.global(qos: .background).async {
                usleep(250000)
                if self.tapCounter > 1 {
                    self.tappedTwice()
                }
                else {
                    self.tappedOnce()
                }
                self.tapCounter = 0
            }
        }
        tapCounter += 1
    }

    func tappedOnce() {
        print("1111111")
    }
    func tappedTwice() {
        print("2222222")
    }
}