2016-11-25 61 views
1

我是新來的目標C.我有一個複選框(它的圖像)在一個表視圖。當這個複選框被點擊時,我想獲得點擊複選框的行(表格行ID)。在複選框中點擊獲取表格行ID - 目標c

我的應用程序看起來像這樣

enter image description here

目標C 我想這樣的,但它總是給我的第一個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]; 

    } 

} 

有人能幫助我獲取點擊複選框的表格行ID。 TNX

+0

我認爲這將有助於你 http://stackoverflow.com/questions/4910323/delegate-from- button-tap-inside-custom-uitableviewcell – nynohu

+0

在創建單元格時將索引號分配給按鈕標籤。 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {cell。 chkButton addTarget:自 動作:@selector(checkBoxBtnPressed :) forControlEvents:UIControlEventTouchUpInside]; 單元格。 chkButton.tag = indexPath.row } – Gnanavadivelu

+0

在你的問題描述中,你說你有複選框(如圖),而你使用複選框作爲按鈕。 請提供您「的cellForRowAtIndexPath」的實施 還請說明您在哪裏如何要使用選定的複選框的indexPath。 (它是單個選擇還是多個選擇)。 –

回答

0

如果只有一個部分,

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

總是給你的第一要素。請嘗試:

PickupTicketsItemObject *item = [ticketList objectAtIndex:indexPath.row]; 
+0

問題不是回合'didSelectRowAtIndexPath',它關於'checkBoxBtnPressed:(id)sender'方法。 –

0

請試試這可能會幫助你。 在的cellForRowAtIndexPath方法寫:

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

btn.tag = indexPath.row + 100; 

} 

並在您的按鈕操作方法:

- (void)checkBoxBtnPressed:(id)sender { 

    UIButton *btn = (UIButton *)sender; 
    NSInteger rowId = btn.tag - 100; 
} 
+0

hy tnx但'cellForRowAtIndexPath'如何知道什麼'btn.tag' –

+0

將按鈕標籤設置爲索引路徑。 – Sanjukta

+0

你可以寫btn.tag = indexPath.row; – Sanjukta

相關問題