gpt4 book ai didi

ios - 如何检测快速触摸或点击的tableView单元格

转载 作者:IT王子 更新时间:2023-10-29 04:58:02 25 4
gpt4 key购买 nike

我正在尝试获取 TableView 中所选项目的 index,然后开始一些事件。不幸的是,我找到的大多数解决方案都在 objective-c 中或不起作用。

方法 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 不要打印 cell 标签..

有人可以帮帮我吗?

import UIKit
import ResearchKit

class TaskListViewController: UIViewController, UITableViewDataSource {

let tasks=[("Short walk"),
("Audiometry"),
("Finger tapping"),
("Reaction time"),
("Spatial span memory")
]


//how many sections are in your table
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

//return int how many rows
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasks.count
}

//what are the contents

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()

var (testName) = tasks[indexPath.row]
cell.textLabel?.text=testName
return cell
}

// give each table section a name

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

return "Tasks"

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let indexPath = tableView.indexPathForSelectedRow();

let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!

println(currentCell.textLabel!.text)
}


override func viewDidLoad() {
super.viewDidLoad()

}
}

尝试几次后,我将代码更改为与我找到的教程不同的代码。而且它也不起作用。现在我认为这是 iOS 模拟器的问题...

import UIKit
import ResearchKit

class TaskListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet
var tableView: UITableView?
var items: [String] = ["We", "Heart", "Swift"]

override func viewDidLoad() {
super.viewDidLoad()

self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView!.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

cell.textLabel?.text = self.items[indexPath.row]

return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(items[indexPath.row])!")
}

}

最佳答案

如果您想要单元格中的值,则不必在 didSelectRowAtIndexPath 中重新创建单元格

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println(tasks[indexPath.row])
}

任务如下:

let tasks=["Short walk",
"Audiometry",
"Finger tapping",
"Reaction time",
"Spatial span memory"
]

您还必须检查您必须设置标识符的 cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell
var (testName) = tasks[indexPath.row]
cell.textLabel?.text=testName
return cell
}

希望对您有所帮助。

关于ios - 如何检测快速触摸或点击的tableView单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31182847/

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