1

I have created a component in XIB file. This file includes 2 components

  1. Label
  2. TableView

I have then linked and set it's File's Owner class to SampleView. I have attached the XIB file view with SampleView.swift file and this file have only following code in it's class:

@IBOutlet var view: UIView!

I have now created a controller file SampleController with protocols UIViewController, UITableViewDelegate and UITableViewDataSource. I have placed the following code in it's init() func to display the custom component:

init() {
        super.init(nibName: nil, bundle: nil)

        modalPresentationStyle = UIModalPresentationStyle.Custom
        view.addSubview(SampleView())
}

I am using this SampleController to programmatically display as a Modal.

These codes does display as the Modal showing Label and TableView. It also populates the data in TableView. The problem is:

When I tap the cell in table, it doesn't trigger the event on first attempt. When I tap another cell then it trigger the previous cell event.

Any idea why is this happening?

Here are 2 functions used for populating and handling cell tap:

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

        var cell = tableView.dequeueReusableCellWithIdentifier("CELL")
        if (cell == nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CELL")
        }
        cell!.textLabel?.text = sampleData[indexPath.row]["title"]
        return cell
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
       print("tapped")
}
codelearner
  • 1,354
  • 1
  • 16
  • 32
  • Souds like a re-use of cells issue. Have you implemented the required `UITableViewDataSource` methods? And if so, can you post them in your question? – koen Mar 30 '16 at 16:15
  • @Koen I have mentioned that I have used UIViewController, UITableViewDataSource and UITableViewDelegate in `SampleController` file. Data is also populating well but the tap event is triggered after tapping the second cell. – codelearner Mar 30 '16 at 16:19
  • Ok, so what's in your `cellForRowAtIndexPath` and `didSelectRowAtIndexPath` functions? – koen Mar 30 '16 at 16:23
  • @Koen I have updated the question with 2 functions. – codelearner Mar 30 '16 at 16:49
  • Darn it! I was using `didDeselectRowAtIndexPath` instead of `didSelectRowAtIndexPath`. That's what will happen when you are programming after midnight. – codelearner Mar 30 '16 at 17:07

2 Answers2

3

Darn it! I was using didDeselectRowAtIndexPath instead of didSelectRowAtIndexPath. That's what will happen when you are programming after midnight.

codelearner
  • 1,354
  • 1
  • 16
  • 32
  • This is such a common mistake. Look at upvotes counts on this post http://stackoverflow.com/a/12587624/3077831 – Wujo Mar 30 '16 at 17:13
0

You need to use a different method to dequeue the cell:

var cell = tableView.dequeueReusableCellWithIdentifier("CELL", forIndexPath: indexPath)

The check for a nil cell is unnecessary, by the way.

TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76
  • I have received this error now: `unable to dequeue a cell with identifier CELL - must register a nib or a class for the identifier or connect a prototype cell in a storyboard` – codelearner Mar 30 '16 at 16:54
  • Yes, you will have to fix that. Search SO and you'll find many similar problems. – koen Mar 30 '16 at 16:59
  • That's not required when you have to create a dynamic cell and using `UITableView` instead of `UITableViewController`. – codelearner Mar 30 '16 at 17:03