Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 439 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UIButton not responding used in a custom UITableViewCell

#11
I came across this issue today, with a button inside a static UITableview cell, that was not responding to user events.
I realised the 'Content View' of the cell also has a 'User Interaction Enabled' tick box. Make sure you select the 'Content View' inside the UITableview cell in your Document Outline menu, then tick the box for 'User Interaction Enabled' in the Attributes Inspector - see attached photo for reference. 'User Interaction Enabled' also needs to be checked for the cell for this to work.
Hope this helps. [XCcode screen shot][1]


[1]:
Reply

#12
I found a simple solution:

Inherits UITableViewCell, and override init()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
//init subviews, eg. self.switch = UISwitch()
super.init(style: style, reuseIdentifier: reuseIdentifier)

// add this line magic code
contentView.isUserInteractionEnabled = true

//add subviews, e.g. self.addSubView(self.switch)
}

Reply

#13
For programmatically created views, the only thing to remember is to declare buttons using `lazy var` in `UITableViewCell`. And also add subviews to `contentView` instead of the cell itself For example:

```
class CounterCell: UITableViewCell {

lazy var incrementButton: UIButton = {
let button = UIButton()
button.setTitle("+", for: .normal)
button.addTarget(self, action: #selector(incrementAction), for: .touchUpInside)
return button
}()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(incrementButton)
// Your constrains here
}

@objc func incrementAction() {
}

}
```

When using programmatically views, there's no need to add `.userInteractionEnabled` flags.

Then to take the action out of the cell, just add a delegate and assign it from the `UITableViewDataSource`.
Reply

#14
I faced a similar issue. I was programmatically adding an `UIButton` to the `UITableViewCell` via `addSubview`. The button would not respond to touch events. Using Debug View Hierarchy, I finally discovered that any subviews added to the `UITableViewCell` was behind `contentView`, which was blocking user input from reaching the `UIButton`. The issue was resolved by adding the `UIButton` to `contentView` instead of the `UITableViewCell`.
Reply

#15
Also, make sure you are adding target actions to your buttons outside their setup. So instead of

let button: UIButton = {
//addTarget...
}()

you can have a function to set up your buttons after something happens:

func setButtonsUp() {
// myButton.addTarget
}
Reply

#16
You only have to do (in ViewDidLoad):

mTableView.delaysContentTouches = false
Reply

#17
I would have `userInteractionEnabled` set to `true` on the table view cell as well. I would prevent taps using the `UITableView` `allowsSelection` to `false`

Also remember to remove the target and action in `tableView:cellForRowAtIndexPath:` since the cells are recycled, the button might already have the target and action, it might add a second.
Reply

#18
I dont know what wrong in the code but i can suggest which i personally use and it works for me

In BranchNearMeTableViewCell.swift

@IBOutlet var btnDetails: UIButton!
@IBAction func btnDetailsClick(sender: AnyObject) {
tapButton?(self)
}
var tapButton: (UITableViewCell -> Void)?

In Table view controller

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("branchNearTableCell") as! BranchNearMeTableViewCell

cell.tapButton = {(user) in
//User will be tablecell here do whatever you want to do here
}

}

So if you click on button in table cell this cell.tapButton will be called you can do whatever you want to do here

Reply

#19
Ad a first sight nothing seems to be wrong with your code.<br>
So I suggest you to add a background color to the superview of the button, why? because if the button is outside the frame of its superview it will never receive touches.<br>
If you see that the button is not inside the background color probably you have an issue positioning the item, check constraints or whatever you are using.
Check also the frame of the button.<br>
You can also do both by inspecting the view at runtime, [here][1] a tutorial.<br>





[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through