paw*_*oon 71 uitableview ios swift
我试图获取index
所选项目TableView
并在此之后开始一些活动.不幸的是,我发现的大多数解决方案都是客观的,或者不起作用.
方法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])!") } }
Ash*_*kad 74
如果您想要来自单元格的值,那么您不必重新创建单元格 didSelectRowAtIndexPath
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println(tasks[indexPath.row])
}
Run Code Online (Sandbox Code Playgroud)
任务如下:
let tasks=["Short walk",
"Audiometry",
"Finger tapping",
"Reaction time",
"Spatial span memory"
]
Run Code Online (Sandbox Code Playgroud)
你还必须检查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
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.
jai*_*jan 36
在Swift 3.0中
您可以通过委托方法找到触摸/单击tableview单元格的事件.同样,可以像这样找到单元格的section和row值.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("section: \(indexPath.section)")
print("row: \(indexPath.row)")
}
Run Code Online (Sandbox Code Playgroud)
spe*_*.sm 12
一些需要发生的事情......
视图控制器需要扩展类型 UITableViewDelegate
视图控制器需要包含该didSelectRowAt
功能.
表视图必须将视图控制器指定为其委托.
下面是一个可以进行分配委托的地方(在视图控制器内).
override func loadView() {
tableView.dataSource = self
tableView.delegate = self
view = tableView
}
Run Code Online (Sandbox Code Playgroud)
并简单实现了该didSelectRowAt
功能.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("row: \(indexPath.row)")
}
Run Code Online (Sandbox Code Playgroud)
paw*_*oon 10
我自己使用weheartswift教程解决了问题
小智 7
这对我很有用:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("section: \(indexPath.section)")
print("row: \(indexPath.row)")
}
Run Code Online (Sandbox Code Playgroud)
输出应该是:
section: 0
row: 0
Run Code Online (Sandbox Code Playgroud)
继承tableview委托和数据源。实现委托您所需要的。
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
Run Code Online (Sandbox Code Playgroud)
最后实现这个委托
func tableView(_ tableView: UITableView, didSelectRowAt
indexPath: IndexPath) {
print("row selected : \(indexPath.row)")
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
82129 次 |
最近记录: |