从UITableView中的选定单元格传递数据 [英] Pass data from a selected cell in UITableView

查看:90
本文介绍了从UITableView中的选定单元格传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tableview,其中每个单元格都填充了数组中的动态数据。从这里开始,当用户选择一行时,我想将特定数据从该单元格推送到ViewController,以便最终可以用于POST请求。现在我知道我的tableviewcontroller上的这段代码只会显示数据

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?

这是我所拥有的片段(实际代码有多个NSMutableArrays,但我只包括一个):

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.

推荐答案

为什么要传递该行的索引?

传递其他控制器需要使用的信息。

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 ;

这篇关于从UITableView中的选定单元格传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆