简体   繁体   English

带Swift的TableViewCell侦听器

[英]TableViewCell Listener with Swift

Good Evening! 晚上好!

I have a specific question about Xcode (Swift) and the TabelView Layout. 我对Xcode(Swift)和TabelView Layout有一个特定的问题。

My App has until now 1 View within a Container in this Container is a UITableViewController embeded which shows a nice static table view. 到目前为止,我的应用程序在此容器中的一个容器中已嵌入1个UITableViewController,它显示了一个不错的静态表视图。

My mistake is how I can register when a User click on one of this table view cells? 我的错误是当用户单击此表视图单元格之一时如何注册?

The method: 方法:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     //CODE TO BE RUN ON CELL TOUCH
}

doesnt work for me - the method is not called, an alert inside of this method is not shown. 对我不起作用-该方法未调用,该方法内部的警报未显示。

Perhaps a false file structure? 也许是错误的文件结构? In my main storyboard I connected a Swift file with the UITableViewController which is inside of the container of the main view controller. 在我的主故事板上,我将一个Swift文件与UITableViewController连接在一起,该UITableViewController位于主视图控制器的容器内部。 Code of the File ist the basic: 文件代码基本:

import UIKit

class test: UITableViewController {

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

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

}

How can I register when a User click on a TableViewCell? 用户单击TableViewCell时如何注册? For example showing an alert? 例如显示警报?

Please help me, I'm really new in iOS development. 请帮助我,我真的是iOS开发的新手。

Thanks! 谢谢!

Here are two pictures of my project-structure: 这是我的项目结构的两张图片:

storyboard-structure 故事板结构

tableView-structure tableView-结构

A functioning table view requires three table view data source methods. 运行正常的表视图需要三种表视图数据源方法。 make sure to add them . 确保添加它们。

func numberOfSectionsInTableView(tableView: UITableView) -> return Int number of section 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->return Int number of rows in section 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> return UITableViewCell that populate the cell data 

hope it help !! 希望对你有帮助!

A really silly mistake... The code was absolutely correct - the problem was in the main.swift file, the file within the container. 一个非常愚蠢的错误...代码是绝对正确的-问题出在main.swift文件中,即容器内的文件。

Above the container I've added a searchBar and used the following code to hide the keyboard by clicking outside the searchbar: https://stackoverflow.com/a/27079103/3849220 在容器上方,我添加了searchBar并使用以下代码通过在搜索栏外部单击来隐藏键盘: https ://stackoverflow.com/a/27079103/3849220

This code crashed my touch Events in the whole View - also "above" the container... dismissKeyboard() was called before the tableView function. 这段代码在整个View中使我的touch Events崩溃了-也在容器“上方” ...在tableView函数之前调用了dismissKeyboard()。

@Paulw11 A big thank you for your example project! @ Paulw11非常感谢您的示例项目! It helps me really much to understand the logic and searching the mistake. 这对我理解逻辑和查找错误非常有帮助。 Thank you! 谢谢!

I replaced the code and write a little own script that hide the keyboard by clicking a "done" button. 我替换了代码,并编写了一个自己的小脚本,通过单击“完成”按钮隐藏了键盘。 Now it works! 现在可以了!

Thanks at all! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM