gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:30:06 25 4
gpt4 key购买 nike

那里!我想知道哪个单元格被点击一次,哪个单元格被点击两次。我有两个类,一个用于 TableViewController,另一个用于 TableViewCell。我想操纵有关触摸的单元格,但我无法获取它们的索引路径。

TableView Controller :

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

}

TableViewCell:

import UIKit

class TableViewCell: UITableViewCell

{

override func awakeFromNib()
{
super.awakeFromNib()

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

let singleTap = UITapGestureRecognizer(target: self, action: #selector(tappedOnce))
singleTap.numberOfTapsRequired = 1
addGestureRecognizer(singleTap)

let doubleTap = UITapGestureRecognizer(target: self, action: #selector(tappedTwice))
doubleTap.numberOfTapsRequired = 2
addGestureRecognizer(doubleTap)

singleTap.require(toFail: doubleTap)
singleTap.delaysTouchesBegan = true
doubleTap.delaysTouchesBegan = true
}

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

@objc func tappedOnce(sender: AnyObject?)
{
print("1111111")
//Memory().reload(reload: x, I: x)
}

@objc func tappedTwice()
{
print("2222222")
}

@IBOutlet weak var Label: UILabel!
@IBOutlet weak var Circle: UIView!

}

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

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

最佳答案

TableViewController:

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

TableViewCell:

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

引用这里 Single and double taps on UITableViewCell in Swift 3

关于swift - 如何识别哪个单元格被点击一次,哪个单元格被点击两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50340099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com