3

I'm really sorry for my english and if I make something wrong, and this is my first question here.

I have a custom UITableViewCell as a view in *.xib file. with an UIImage in it and a button on that UIImage. I connected this button with an outlet to my CustomTableViewCell.swift class.

@IBOutlet weak var authorImage: UIImageView!
@IBOutlet weak var authorButton: UIButton!

In the MyTableViewController.swift I've registered a nib

tableView.registerNib(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "my_cell")

And wrote an cellForRow... function like this:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("my_cell", forIndexPath: indexPath) as! CustomTableViewCell
    cell.authorButton.addTarget(self, action: "authorButtonPressed:", forControlEvents: .TouchUpInside)
    return cell
}

And, finally I have this function (which I use in addTarget...):

func authorButtonPressed (sender:UIButton!) {
    print("Hi!")//line1
    print(sender)
}

and a breakpoint on the first line. But this function is never called.

And I have also just understood that button isn't animated, when I tap it.

How can I fix it or find a way to the solution?

Thank you in advance

Nikolay Pryahin
  • 377
  • 5
  • 19
  • And you're 100% sure the button is on top of the image and not behind it? If you remove/hide the image temporarily, does the problem still exist? – j.f. Oct 26 '15 at 20:47
  • @j.f. I've just deleted everything around the button. But it still doesn't change, when I tap it. And the authorButtonPressed: function does't start the execution. I've also tried to use the view hierarchy debugger to see, if something can be over my button, but the only thing that is over the button is UITableViewCellContentView. Here is the image: https://www.dropbox.com/s/qb08tm08l5i0i9a/%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202015-10-26%20%D0%B2%2023.55.09.png?dl=0 – Nikolay Pryahin Oct 26 '15 at 21:02
  • Could you post a screenshot of your xib (with the document outline)? – joern Oct 26 '15 at 21:09
  • @joern I've already deleted absolutely everything on the cell but my button https://www.dropbox.com/s/be8p26d7hbqy1qs/%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202015-10-27%20%D0%B2%200.21.14.png?dl=0 – Nikolay Pryahin Oct 26 '15 at 21:25

4 Answers4

4

You are using a UIView as the root view in your nib. For a custom UITableViewCell you need to make the root view a UITableViewCell:

enter image description here

So, you have to create an empty xib and than drag a UITableViewCell on it. Then start adding your subviews to the ContentView

joern
  • 27,354
  • 7
  • 90
  • 105
0

You shouldn't add add target on this function:

override func tableView(tableView: UITableView, cellForRowAtIndexPath    indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("my_cell", forIndexPath: indexPath) as! CustomTableViewCell
**cell.authorButton.addTarget(self, action: "authorButtonPressed:", forControlEvents: .TouchUpInside)**
return cell
}

You just add outlet action into your cell. It will be fine. Like this code:

@IBAction func testPressed(sender: AnyObject) {
    print("test")
}
vien vu
  • 4,277
  • 2
  • 17
  • 30
0

follow joern solution, after that select Table View Cell by clicking on it and make sure that the check mark should be enabled of the User Interaction Enabled attribute of the attribute inspector pane.

Krishnarjun Banoth
  • 1,410
  • 1
  • 15
  • 30
0

Did you use this magic button? enter image description here

Thanks to it you can see your layer tree and it might content something weird... like this. enter image description here

A view over the rest of them which is doing mess there. Just investigate, mate!

oskarko
  • 3,382
  • 1
  • 26
  • 26