简体   繁体   中英

How to get an ID for an individual cell in a TableView

In my app I'm using a Web Service to populate my table view of jobs. When they click on a job, the app brings the user to the DetailsViewController which displays the job details. So far, I'm hardcoding the job ID to get the job details by doing the following:

- (void)viewDidLoad
{
[super viewDidLoad];



NSURL *url = [NSURL URLWithString:properUrlString];

NSData *data = [NSData dataWithContentsOfURL:url];

NSError *error;

NSMutableArray *jobsCallArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSString *currentId = @"usajobs:364160700";
for (NSDictionary *theJob in jobsCallArray)
{

  if([currentId isEqualToString:theJob[@"id"]])
  {
      jobTitle.text = theJob[@"position_title"];
      companyName.text = theJob[@"organization_name"];
      minimum.text = theJob[@"minimum"];
      maximum.text = theJob[@"maximum"];
  }



}


// Do any additional setup after loading the view.
}

Now I would like for the "currentID" to equal the ID for each individual cell. How would I go about doing that?...

All help is appreciated,

Thanks in advance.

I'm not sure I'm following you, but here is what I understand of your question.
How can I find back the currentID for the cell the User have selected ?

There is 2 important method for UITableView
one in UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
the other in UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

You are probably keeping an Array of jobs that allow you to populate your TableViewCell in -tableView:cellForRowAtIndexPath: using the indexPath to find the right index in your array.
Do the same logic in your -tableViewDidSelectRowAtIndexPath to get back the selected job.


You can ask the table view for the selected cell with this method :
- (NSIndexPath *)indexPathForSelectedRow

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