Detect Tap on a Button in Uitableviewcell for Uitableview Containing Multiple Sections

Detect tap on a button in UITableViewCell for UITableView containing multiple sections

Objective-C

-(void)addItem:(UIButton*) sender
{

CGPoint touchPoint = [sender convertPoint:CGPointZero toView:mainTable]; // maintable --> replace your tableview name
NSIndexPath *clickedButtonIndexPath = [mainTable indexPathForRowAtPoint:touchPoint];

NSLog(@"index path.section ==%ld",(long)clickedButtonIndexPath.section);

NSLog(@"index path.row ==%ld",(long)clickedButtonIndexPath.row);


}

Swift3

 func addItem(sender: UIButton)
{
var touchPoint = sender.convert(CGPoint.zero, to: self.maintable)
// maintable --> replace your tableview name
var clickedButtonIndexPath = maintable.indexPathForRow(at: touchPoint)
NSLog("index path.section ==%ld", Int(clickedButtonIndexPath.section))
NSLog("index path.row ==%ld", Int(clickedButtonIndexPath.row))


}

Swift2 and above

func addItem(sender: UIButton)
{
var touchPoint: CGPoint = sender.convertPoint(CGPointZero, toView: mainTable)
// maintable --> replace your tableview name
var clickedButtonIndexPath: NSIndexPath = mainTable(forRowAtPoint: touchPoint)
NSLog("index path.section ==%ld", Int(clickedButtonIndexPath.section))
NSLog("index path.row ==%ld", Int(clickedButtonIndexPath.row))
}

Populate a UItableView with new sections when button tapped

I try to answer you as far as I understood what you need.

So we want to be able to have more than one section. So we need a variable to make it possible to change the number of sections in the tableView.

var numberOfSections: Int = 1

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return numberOfSections
}

That way we can change numberOfSections depending on our needs to have 1 or 2 sections in our TableView.

Then we need to have more than one Array to define the numberOfRowsInSection. Otherwise we will have the exact same elements per section. If we use your current version. Both sections would have the exact same content.

var sectionOneArray = [String]()
var sectionTwoArray = [String]()

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if section == 0 {
return sectionOneArray.count
}

if section == 1 {
return sectionTwoArray.count
}

return 0
}

We'll also need two customer Strings to fill the titleForHeaderInSection.

var myFirstCustomer = ""
var mySecondCustomer = ""

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

if section == 0 {
return myFirstCustomer
}

if section == 1 {
return mySecondCustomer
}
}

Now we can populate the arrays and tell our App if we want one or two sections depending on the amount of customers like that. We also need to have a possibility to tell if we choose the Menus in the Popup for Customer 1 or Customer 2, but I currently have no idea what your Popup looks like, but the code would be something like that:

func foodselected(valuesent:String) { 

if popUpChoseForCustomerOne {
let meal = valuesent

sectionOneArray.append(meal)
}

if popUpChoseForCustomerTwo {
let meal = valuesent

sectionTwoArray.append(meal)
numberOfSections = 2
}

self.TableView.reloadData()

}

I did this code without IDE, so it might not be perfect. But it should definitely give you the idea of how I would handle the issue.

Get button click inside UITableViewCell

1) In your cellForRowAtIndexPath: method, assign button tag as index:

cell.yourbutton.tag = indexPath.row;

2) Add target and action for your button as below:

[cell.yourbutton addTarget:self action:@selector(yourButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

3) Code actions based on index as below in ViewControler:

-(void)yourButtonClicked:(UIButton*)sender
{
if (sender.tag == 0)
{
// Your code here
}
}

Updates for multiple Section:

You can check this link to detect button click in table view for multiple row and section.

Tell to a button which row of the tableView we are tapping

In your cellForRow method assign tag to a button like shown below:

yourCell.yourButton.tag = indexPath.row

Then assign selector to it in cellForRow method.

yourCell.yourButton.addTarget(self, action: #selector(myClass.rowReload(_:)), forControlEvents: .TouchUpInside)

And in your rowReload method argument sender should have type UIButton and your method will be:

func rowReload(_ sender: UIButton) {

then you can access its tag and your IndexPath will be:

let index = IndexPath(row: sender.tag, section: 0)

UITableViewCell Buttons with action

I was resolving this using a cell delegate method within UITableViewCell's subclass.

Quick overview:

1) Create a protocol

protocol YourCellDelegate : class {
func didPressButton(_ tag: Int)
}

2) Subclass your UITableViewCell (if you haven't done so):

class YourCell : UITableViewCell
{
var cellDelegate: YourCellDelegate?
@IBOutlet weak var btn: UIButton!
// connect the button from your cell with this method
@IBAction func buttonPressed(_ sender: UIButton) {
cellDelegate?.didPressButton(sender.tag)
}
...
}

3) Let your view controller conform to YourCellDelegate protocol that was implemented above.

class YourViewController: ..., YourCellDelegate {  ... }

4) Set a delegate, after the cell has been defined (for reusing).

let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! YourCell
cell.cellDelegate = self
cell.btn.tag = indexPath.row

5) In the same controller (where is your implemented UITableView delegate/datasource), put a method from YourCellDelegate protocol.

func didPressButton(_ tag: Int) {
print("I have pressed a button with a tag: \(tag)")
}

Now, your solution is not tag / number dependent. You can add as many buttons as you want, so you are ready to get response via delegate regardless how many buttons you want to install.

This protocol-delegate solution is preferred in iOS logic and it can be used for other elements in table cell, like UISwitch, UIStepper, and so on.



Related Topics



Leave a reply



Submit