Talha Aslam
Talha Aslam

Reputation: 51

Custom Cell button Click

My scenario is as follow's:

Custom Cell View

enter image description here

1) I am populating data from server in the custom cell view which is perfectly populated, i want a link store in my array which i want to open in browser, so i want to check which index.row button is clicked so that against that row i can get the url link and open it in browser, so far i have found the solution of button tags but that doesn't work's as well, as if we have two cell's in the screen at same time on click of button both button return's same tag.

2) As the image attached i have another storyboard in which i want to segue to a new view controller, same as mentioned above, i want to pass a specific post title and key as well.

I hope every thing is clear.

Thank's in advance.

Upvotes: 1

Views: 2547

Answers (4)

Manan Devani
Manan Devani

Reputation: 434

I have found the solution of button tags but that doesn't work's as well

Perform following steps to detect which button is selected.

  1. Type the code snippet given below in cellForRowAtIndexPath: method.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        // DEFINE CELL
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
        // SET ACTION METHOD ON CALL BUTTON
        UIButton *button = (UIButton *) [self.view viewWithTag:40];
    
        [button setUserInteractionEnabled:YES];
    
        [button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventAllTouchEvents];
    
    }
    
  2. Add following method for checkButtonTapped:.

    - (void)checkButtonTapped:(UIButton *)sender
    {
        // GET POINT OF THE BUTTON PRESSED
        CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    
        // GET INDEXPATH FOR THE CELL WHERE THE BUTTON IS PRESSED
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    
        if (indexPath != nil)
        {
            //Use indxPath.row to get item
            //Perform operations
        }
    }
    

There is another approach if you want to perform action according to the cell selected by the user.

For the case where you want to check which row of the cell is clicked you should use following method.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     NSLog(@"ShowindexPath%ld", (long)indexPath.row); }

Here we check for the user interaction on cell. indexPath.row will return index number for the cell selected by user. You can perform other actions in place of NSLog method.

For the purpose to send value while performing segue use following steps:

  • Create property in the NewViewController.h file where you want to perform segue as follow:

    @property (nonatomic) NSString* receivedValue;
    
  • @synthesize property by writing following code in NewViewController.m file within @implementation and @end

    @synthesize receivedValue;
    
  • ImportNewViewController.h file in TableViewController.m file.

  • Go to prepareForSegue: method in the TableViewController.m file from where you want to send the value and write following code according to your class names within it.

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NewViewController *nVC = [segue destinationViewController];
    [nVC setReceivedValue:sendValue];
    }
    

Here sendValue is the value that you want to send.

That's all. Build and run your project and enjoy.

Upvotes: 0

Shubham bairagi
Shubham bairagi

Reputation: 953

you can add target for button in cellForRowAtIndexPath like :

 [cell.yourButton addTarget:self action:@selector(buttonAction:event:) forControlEvents:UIControlEventTouchUpInside];

here you can receive the action for this button

-(void)buttonAction:(UIButton *)sender event:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPos = [touch locationInView:self.tblview];
NSIndexPath *indexPath = [self.tblview  indexPathForRowAtPoint:touchPos];
if(indexPath != nil){

// handle click event..
    }
}

surely it will work.........

Upvotes: 0

CoderKK
CoderKK

Reputation: 361

You can extend UITableViewCell and add a touple property to it. key containing the index.row and value containing the URL eg:- 1:"www.stackoverflow.com". When you click on a button, all you have to figure out is the index.row and from that you can get the value that you are looking for.

Upvotes: 0

beyowulf
beyowulf

Reputation: 15331

To answer the first part of your question. Tags are a pain. It's better to pass along the UIButton as the sender, and, in the action for the button, then you can say something like:

-(void) buttonPresse:(UIButton *)sender
{
    CGPoint location = [self.tableView convertPoint:sender.bounds.origin fromView:sender];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
    UITableViewCell *cell = [self.table cellForRowAtIndexPath];
    //Do what you want with the cell or indexPath

}

Upvotes: 1

Related Questions