iphone - 从 UITableView 中的选定单元格传递数据

标签 iphone objective-c ios uitableview

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

- (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];

}

但我如何实际使用这些数据?

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

     - (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;

如果能帮助我朝着正确的方向前进,那就太棒了。如果有任何不清楚的地方,请告诉我。

最佳答案

为什么要传递行的索引?
传递其他 Controller 需要使用的信息。

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

关于iphone - 从 UITableView 中的选定单元格传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8640424/

相关文章:

ios - 如何在我的 iOS 应用程序中实现该功能?

ios - 在 View Controller Containment 中保留对 Parent 的引用

iphone - 使用 writeImageToSavedPhotosAlbum 将 UIImage 保存到相册

ios - Sprite 未从场景 iOS 中移除

iphone - 核心图示例文件夹出现问题(示例不起作用)

iphone - "Plain Style unsupported in a Navigation Item"警告我的自定义条形按钮项目

iPhone 连接到本地网络计算机上的 MySQL 数据库

ios - 呈现 iOS HealthKit 权限模态视图时,其后面的 View 为黑色

ios - 使用 CollectionView 删除 Core Data 中的记录

iphone - 如何自定义 UITableViewCell 而不会在 UITableView 中出现任何卡住