問題描述
在復選框上單擊獲取表行 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 Baman、Alp、Sanjukta)