sol*_*ing 11 uitableview ios swift
我正在使用Swift创建一个应用程序.我有一个UITableView我填充数据库中的一些数据.当用户点击一个单元格时,我想触发一个动作.
我做了什么 :
var array: [String] = ["example"]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell(style :UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel.text = array[indexPath.row]
cell.tag = indexPath.row
cell.targetForAction("getAction:", withSender: self)
return cell
}
func getAction(sender:UITableViewCell)->Void {
if(sender.tag == 0) {
println("it worked")
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试从另一个帖子调整解决方案,但我做错了.先感谢您
Raj*_*tia 22
实施UITableViewDelegate和使用didSelectRowAtIndexPath
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
Run Code Online (Sandbox Code Playgroud)
您可以使用它indexPath从您的收藏中获取该项目并执行您的操作
Lua*_*ico 10
在Swift 4中,我相信这个方法已经发生了如此微小的改变:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// your code
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19966 次 |
| 最近记录: |