在復選框上單擊獲取表行 ID - 目標 c (On check box click get the table Row ID - Objective c)


問題描述

在復選框上單擊獲取表行 ID ‑ 目標 c (On check box click get the table Row ID ‑ Objective c)

我是目標 C 的新手。我在表格視圖中有一個複選框(它的圖像)。每當單擊此復選框時,我都想獲取單擊的複選框的行(表行 ID)。

我的應用程序如下所示

在此處輸入圖片描述

目標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


參考解法

方法 1:

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];

方法 2:

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;
}

(by Sathya BamanAlpSanjukta)

參考文件

  1. On check box click get the table Row ID ‑ Objective c (CC BY‑SA 2.5/3.0/4.0)

#objective-c #iOS #checkbox #uitableview






相關問題

為什麼 NSWindow 或 NSView 實例處理它自己的鍵事件,而不是它的委託? (Why does an NSWindow or NSView instance handle its own key events, and not its delegate?)

在調用之前釋放了 didFinishSelector ASIHTTPRequest 的自定義委託類 (Custom delegate class for didFinishSelector ASIHTTPRequest being deallocated before called)

通過 IKImage 視圖預覽圖像 (Previewg images via IKImage View)

Gaya Jendela Kustom di Kakao (Custom Window Style in Cocoa)

Тонкія белыя лініі дадаюцца пры абрэзцы выявы (Objective-C OSX) (Thin white lines being added when cropping an image (Objective-C OSX))

Obj-C:遞歸:在堆棧上創建新對像或使用可變性 (Obj-C: Recursion: Making new objects on stack or use mutability)

XCode 4.6 顯示方法 + (void)beginAnimations:(NSString *)animationID context:(void *)context; 的內存洩漏警告 (XCode 4.6 shows a warning of memory leak for method + (void)beginAnimations:(NSString *)animationID context:(void *)context;)

單元格中不可見的 UITextField 已將 userInteractionEnabled 設置為 No (UITextField in cell that is not visible has userInteractionEnabled set to No)

Gửi nhật ký sự cố ứng dụng / nhật ký ứng dụng thông thường (Send app crash logs/regular app logs)

我的鍵盤默認值僅在我的應用程序中更改 (My keyboard default be change in only my App)

在復選框上單擊獲取表行 ID - 目標 c (On check box click get the table Row ID - Objective c)

iPhone SDK:檢查 UITableView 中每個對象的 ID 號並相應地更改徽章號 (iPhone SDK: Checking the ID number for each object in a UITableView and change the badge number accordingly)







留言討論