我是目标C的新手。我在表格视图中有一个复选框(它是一个图像)。当单击此复选框时,我想获取所单击复选框的行(表行标识)。
我的应用程序看起来像这样
目标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 :(得分:0)
如果您只有一个部分,
PickupTicketsItemObject *item = [ticketList objectAtIndex:indexPath.section];
始终为您提供第一个元素。尝试:
PickupTicketsItemObject *item = [ticketList objectAtIndex:indexPath.row];
答案 1 :(得分: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;
}