0

Good Evening!

I have a specific question about Xcode (Swift) and the 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.

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. 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? For example showing an alert?

Please help me, I'm really new in iOS development.

Thanks!

Here are two pictures of my project-structure:

storyboard-structure

tableView-structure

  • I which view controller did you implement the `didSelectRowAtIndexPath`? Why did you use a container view and tableviewcontroller instead of a table view? – Paulw11 Aug 07 '16 at 22:42
  • Thanks for your reply! I implement the "Listener method" in the test.swift file, the file which is connect with the UITableViewController. That doesnt work... I use the external controller because I need to use a static TableView with groups. –  Aug 07 '16 at 22:56
  • Have you set the custom subclass of your UITableViewController to `Test` ? – Paulw11 Aug 07 '16 at 23:13
  • Yes, I create a new file / ios / swift file and fill it with the content above. After that i connect the View (List) on the storyboard with this file, set the module to myApp Name and save it. All correct? –  Aug 07 '16 at 23:32
  • I just set up a quick test project and it worked correctly, so you have something wrong. Double check the custom class for your UITableViewController scene; it should be `test` (It really should be Test since by convention classes start with an uppercase letter but it won't stop it working). The problem you have described would happen if the custom class was still the default `UITableViewController` – Paulw11 Aug 07 '16 at 23:43
  • Custom Class is definetly test (not uppercase). The connection between swift file and View is correct, i've change the color of a label inside of the TableView from the test file to test it. –  Aug 07 '16 at 23:56
  • Your last sentence I dont understand... Do you mean the default UITableViewController is set when the connection to my test file is false? Is there defintely a problem with my file? Not a false structure of the TableView etc.? –  Aug 07 '16 at 23:59
  • I mean when you add a UITableViewController scene to your storyboard, it's class will be `UITableViewController`. You need to override this to `test` to have your code executed. The symptom you are seeing of your delegate method not executing would be explained if the class was wrong, since your class would not be instantiated. Add a `print` statement to `viewDidLoad` in test – Paulw11 Aug 08 '16 at 00:01
  • I have uploaded my test project here - https://github.com/paulw11/TableViewControllerTest You can compare – Paulw11 Aug 08 '16 at 00:15
  • Thanks for your example project - it helps me really much! I've written an answer to my question with a decription of the mistake. It was really silly ;-). –  Aug 08 '16 at 14:08

2 Answers2

0

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

MedAmine.Rihane
  • 1,193
  • 10
  • 17
0

A really silly mistake... The code was absolutely correct - the problem was in the main.swift file, the file within the container.

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

This code crashed my touch Events in the whole View - also "above" the container... dismissKeyboard() was called before the tableView function.

@Paulw11 A big thank you for your example project! 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!

Community
  • 1
  • 1