Pass data from a selected cell in UITableView

2020-04-16 01:55发布

问题:

I have a tableview where each cell is populated with dynamic data from an array. From here, when the user selects a row, I want to push the particular data from that cell to a ViewController so that it can eventually be used for a POST request. Now I know this code on my tableviewcontroller will just display the data

- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath {
    DetailGameController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
    [self.navigationController pushViewController:detail animated:YES];
    NSUInteger row = [indexPath row];
    detail.rowNum.text = [photoTitles objectAtIndex:row];

}

but how do I actually use the data?

Here is a snippet of what I have (the actual code has multiple NSMutableArrays but I only included one):

     - (void)viewDidLoad
    {

        [super viewDidLoad];
        photoTitles = [[NSMutableArray alloc] init];
 NSURL *url = [NSURL URLWithString:@"http://localhost/test.php"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:dataCenter.data forKey:@"name"];
    [request setDelegate:self];
    [request startAsynchronous];

}
- (void)requestFinished:(ASIFormDataRequest *)request
{

    NSString *responseString = [request responseString];

    NSDictionary *dictionary = [responseString JSONValue]; 
    NSArray *photos = [[dictionary objectForKey:@"photos"] objectForKey:@"photo"];

    for (NSDictionary *photo in photo) {
        NSString *photo = [photo objectForKey:@"photo"];
        [photoTitles addObject:photo];
    }    
}

-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
    return [photoTitles count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   {


    static NSUInteger const kPhotoTitleTag = 2;
    UILabel *photoTitleLabel = nil;

 static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             SimpleTableIdentifier];
    if(cell == nil){


        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: SimpleTableIdentifier];

        photoTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 25, 170, 14)];
        [cell.contentView addSubview:photoTitleLabel];
}else {
        photoTitleLabel = (UILabel *)[cell.contentView viewWithTag:kPhotoTitleTag];
}
NSUInteger row = [indexPath row];

    photoTitleLabel.text = [photoTitles objectAtIndex:row];
    return cell;

Any help in putting me in the right direction would be awesome. Let me know if anything isn't clear.

回答1:

Why pass the index of the row?
Pass the information the other controller need to use.

DetailGameController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
NSUInteger row = [indexPath row];
detail.someProperty = [photoTitles objectAtIndex:row];
detail.someOtherProperty = // other interesting elements ;