1

I am new to objective C. I have a checkbox (its a image) inside a table view. when ever this checkbox is clicked i want get the row (table row id) of the clicked check box.

My application looks like this

enter image description here

objective c I tried like this but it always gives me the first id

- (void)checkBoxBtnPressed:(id)sender {

    UIButton *btn = (UIButton *)sender;
    PickupTicketsItemObject *item = [ticketList objectAtIndex:btn.tag];

    NSLog(@"item_select %@", item.getTicket_id);
    if ([item isSelected]) {
        [btn setBackgroundImage:[UIImage imageNamed:@"icon_uncheck"] forState:UIControlStateNormal];
        [item setIsSelected:NO];
    }else {
        [btn setBackgroundImage:[UIImage imageNamed:@"icon_check"] forState:UIControlStateNormal];
        [item setIsSelected:YES];
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    NSLog(@"selected index : %@", indexPath);
    NSLog(@"selected index : %ld", (long)indexPath.section);

    //Check if assigned to user

    PickupTicketsItemObject *item = [ticketList objectAtIndex:indexPath.section];
    NSLog(@"selected index : %@", item.ticket_id);
    NSLog(@"selected index : %@", item.ticket_assignedto);

    //Print all user defaults key and value
    //NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);

    // get the display name from user defaults
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    NSString *displayName;
    NSString *AgentName;
    if ([userDefaults objectForKey:@"DisplayName"] != nil) {
        displayName = [userDefaults objectForKey:@"DisplayName"];
    }else {  displayName = @"Not defined";  }

    AgentName = [@"Agent : " stringByAppendingString:displayName];

    NSLog(@"Display Name  : %@", displayName);
    NSLog(@"Agent Name    : %@", AgentName);
    NSLog(@"Assigned Name : %@", item.ticket_assignedto);


    // ticket is already assigned to agent, send to ticket details page
    if ([item.ticket_assignedto isEqualToString:AgentName]) {
        NSLog(@"Ticket already assigned to this User.");
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        TicketDetailViewController *ticketDetailViewController = [storyboard instantiateViewControllerWithIdentifier:@"TicketDetailViewController"];
        ticketDetailViewController.ticket_id = item.ticket_id;

        [kNavigationController pushViewController:ticketDetailViewController animated:YES];
        [kMainViewController hideLeftViewAnimated:YES completionHandler:nil];

    } else{
        NSLog(@"Ticket not assigned to this User.");
        // Ask the user to pick the ticket.

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        SinglePickUpViewController *singlePickUpViewController = [storyboard instantiateViewControllerWithIdentifier:@"SinglePickupVC"];
        singlePickUpViewController.ticket_id = item.ticket_id;
        [kNavigationController pushViewController:singlePickUpViewController animated:YES];
        [kMainViewController hideLeftViewAnimated:YES completionHandler:nil];

    }

}

Can some one help me to get the clicked checkboxes table row id. tnx

Sathya Baman
  • 3,424
  • 7
  • 44
  • 77
  • I think it will help you http://stackoverflow.com/questions/4910323/delegate-from-button-tap-inside-custom-uitableviewcell – nynohu Nov 25 '16 at 07:36
  • Assign index number to button tag while cell creation. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { [cell. chkButton addTarget:self action:@selector(checkBoxBtnPressed:) forControlEvents:UIControlEventTouchUpInside]; cell. chkButton.tag = indexPath.row } – Gnanavadivelu Nov 25 '16 at 07:56
  • In your question description you said you have checkbox (as Image), while you are using checkbox as Button. Please provide your implementation of 'cellForRowAtIndexPath' Also please explain how and where you want to use that indexPath of Selected checkbox. (Is it a single selection or Multiple Selection). – Piyush Sharma Nov 25 '16 at 08:05
  • you have multiple section in your tableview? – jignesh Vadadoriya Nov 25 '16 at 09:05

2 Answers2

0

If you have only one section,

PickupTicketsItemObject *item = [ticketList objectAtIndex:indexPath.section];

always gives you the first element. Try:

PickupTicketsItemObject *item = [ticketList objectAtIndex:indexPath.row];
Alp
  • 1
0

Please try this it may help you. In cellForRowAtIndexPath method write this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

btn.tag = indexPath.row + 100;

}

And in your button action method:

- (void)checkBoxBtnPressed:(id)sender {

    UIButton *btn = (UIButton *)sender;
    NSInteger rowId = btn.tag - 100;
}
Sanjukta
  • 1,057
  • 6
  • 16