jeudi 24 novembre 2016

On check box click get the table Row ID - Objective c

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




Aucun commentaire:

Enregistrer un commentaire