简体   繁体   中英

ios - how to get id full array value from a UITableView cell?

I am able to get the text value of a cell like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSUInteger *row = [indexPath row];

    // NOW GET THE NAME AND ID FROM THAT ROW
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = cell.textLabel.text;    

    NSLog(cellText);    
}

but I need to be able to get the id of that item, or at least use the row to get the item from the array.

Here is how I get the array for the UITableView populated:

@synthesize items_array;

and later:

                 items_array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

                 [self.itemList reloadData];

and the array has JSON which has for every item, fields like item_id, and item_name

So when the user clicks on an item from the list, how can I get the id field?

Thanks!

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *idField = [items_array objectAtIndex:indexPath.row];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSDictionary *itemInfo = [items_array objectAtIndex:indexPath.row];
    NSString *itemID = [itemInfo objectForKey:@"item_id"];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM