Button in Uitableviewcell Not Responding Under iOS 7

Button in custom UITableViewCell not responding in iOS 7

So apparently the issue was these lines of code(in diff format from my git diff output):

--(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
+-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

and

--(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
+-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

so yeah... seems that in iOS7+ if you have the "old" output types for these datasource methods it'll look fine but bork your tableview functionally... good times, but hey it's fixed now^^ hopefully this helps others.

UIButton not responding used in a custom UITableViewCell

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.

Custom UIButton TableView not responding

So the actual solution went that way:
at first iv'e changed my class to UIView, which make things way more convenient in terms of adding more views or tables in my case. After that i made delegate for the opening of the table and in the viewController that using the button i made an height constraint outlet to the button and updated it when that button open the collapse table.
Thanks to @DonMag for guiding me to investigate the solution.

I will try make it more simple and easy to use but until then,
here's the semi final buttonView class:

//
// CollapseView.swift
// IBITrade
//
// Created by Matan Levi IBI on 22/12/2020.
//

import UIKit

protocol CollapseViewDelegate
{
func setTableViewOptions(collapseView : CollapseView) -> [String]
func didSelectRow(collapseView : CollapseView, tableView: UITableView, indexPath: IndexPath)
func isCollapseOpen(collapseView : CollapseView, isOpen : Bool)
}

@IBDesignable class CollapseView: UIView {

@IBOutlet weak var tableViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var collapseBtnOutlet: UIButton!
@IBOutlet weak var collapseTableViewOutlet: UITableView!

public var buttonTitle: String = "" {
didSet {
self.collapseBtnOutlet.setTitle(buttonTitle, for: .normal)
}
}

var delegate : CollapseViewDelegate?
let open = CGFloat(113)
let close = CGFloat.zero
var isOpen = false

// init used if the view is created programmatically
let nibName = "CollapseView"
var contentView:UIView?

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
guard let view = loadViewFromNib() else { return }
view.frame = self.bounds
self.addSubview(view)
contentView = view

}
func loadViewFromNib() -> UIView? {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiate(withOwner: self, options: nil).first as? UIView
}


@IBAction func openCloseTable(_ sender: UIButton) {
collapseTableViewOutlet.addBorder()
UIView.animate(withDuration: 1) { [self] in
self.tableViewHeightConstraint.constant = self.isOpen ? self.close : self.open
sender.imageView?.transform = self.isOpen ? CGAffineTransform.identity : CGAffineTransform(rotationAngle: .pi)
delegate?.isCollapseOpen(collapseView: self, isOpen: isOpen)
self.layoutIfNeeded()
}

isOpen = !isOpen
}

}
extension CollapseView : UITableViewDelegate, UITableViewDataSource
{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return delegate?.setTableViewOptions(collapseView: self).count ?? 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = delegate?.setTableViewOptions(collapseView: self)[indexPath.row]
cell.backgroundColor = .green
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.didSelectRow(collapseView: self, tableView: tableView, indexPath: indexPath)
}

}

On a custom UITableViewCell with a button the text disappears sometimes - iOS 7

Try removing the PrepareForReuse method in your ButtonCellNew class.

I had similar issues with it on iOS 7 and I removed it completely. It seems as its behavior has somewhat changed, as it's called by the system right before the cell is returned from the DequeueReusableCell method. Apparently, the "new" behavior includes calling the method at some other point also. Or... it's a bug or something.

It's not that important anyway, as Apple only suggests using it for resetting UI-related properties and not content-related properties.

So you can live without:

Button.TitleLabel.Text = string.Empty;

UIButton not responding to click event in UITableViewCell

you have not given any control event to button .Change it to UIControlEventTouchUpInside from UIControlStateNormal

Custom buttons in XIB used as Custom UITableViewCell don't respond to taps (ios7)

Solved with:

[cell.contentView setUserInteractionEnabled: NO];

Custom UITableViewCell, button in cell not working

I would recommend you to go through this link. It has clear description of your problem. However, I've tried to extract needed info.

Many types of events rely on a responder chain for event delivery. The responder chain is a series of linked responder objects. It starts with the first responder and ends with the application object. If the first responder cannot handle an event, it forwards the event to the next responder in the responder chain.

A responder object is an object that can respond to and handle events. The UIResponder class is the base class for all responder objects, and it defines the programmatic interface not only for event handling but also for common responder behavior. Instances of the UIApplication, UIViewController, and UIView classes are responders, which means that all views and most key controller objects are responders.

In your Case, when you are writing event methods in your customTableViewCell class, your methods are not being called. this is because, TableViewCell is not participating in Responder chain.

next thing is, check whether "User Interaction is enabled " or not for your Cell. You can find it under View section of "Attribute inspector".



Related Topics



Leave a reply



Submit