ios - 如何在选择的 UITableViewCell 行中传递值

标签 ios objective-c iphone json uitableview

我有 UITableViewCell,明智的单元格索引类别和扩展单元格单击标题,并通过 JSON 加载数据。我想选择单元格并将数据传递给另一个 View Controller 。我尝试了很多次,但只在第一个单元格中传递数据。我单击了另一个单元格,因此此条件将数据传递给第一个单元格。无法发送特定的单元格数据。怎么可能。请帮忙。

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError *error;

    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves  error:&error];

    NSArray *statuses = [json objectForKey:@"status"];
    names=[[NSMutableArray alloc]initWithArray:[statuses valueForKey:@"business_category_name"]];
    business_details_array=[[NSMutableArray alloc]initWithArray:[statuses valueForKey:@"business_details"]];

    for (int i=0; i<[names count]; i++) {
        [arrayForBool addObject:[NSNumber numberWithBool:NO]];
    }

    [self.tabel_view reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     return names.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([[arrayForBool objectAtIndex:section] boolValue]) {
        return [[business_details_array objectAtIndex:section] count];
    }
    else
        return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 70;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *sectionHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,70)];

    sectionHeaderView.backgroundColor=[UIColor grayColor];

    sectionHeaderView.tag=section;

    UIView *sectionsubHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,60)];

    sectionsubHeaderView.backgroundColor=[UIColor blueColor];

    UIImageView *arrow=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,60, 60)];

    [arrow setImage:[UIImage imageNamed:@"arrow.png"]];

    UILabel *Lbl=[[UILabel alloc]initWithFrame:CGRectMake(60, 0,tableView.frame.size.width-60, 60)];

    Lbl.text=[names objectAtIndex:section];

    Lbl.textColor=[UIColor whiteColor];

    [sectionsubHeaderView addSubview:arrow];

    [sectionsubHeaderView addSubview:Lbl];

    [sectionHeaderView addSubview:sectionsubHeaderView];

    UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];

    [sectionHeaderView addGestureRecognizer:headerTapped];

    return  sectionHeaderView;
}

- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
{

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
    if (indexPath.row == 0) {
        BOOL collapsed  = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
        for (int i=0; i<[names count]; i++) {
            if (indexPath.section==i) {
                [arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:!collapsed]];
            }
        }
        [self.tabel_view reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];

    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MemberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ht"];
    if (cell==nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    NSString*title_str=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"name"] objectAtIndex:indexPath.row]];
    cell.title.text= title_str;
    [titles addObject:title_str];
    NSLog(@"get %@",titles);

    cell.email.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"email"] objectAtIndex:indexPath.row]];
    //[emailary addObject:cell.email.text];

    cell.address_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"address"] objectAtIndex:indexPath.row]];
    //[adrsary addObject:cell.address_lbl.text];

    cell.phone_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"phone"] objectAtIndex:indexPath.row]];
    // [phoneary addObject:cell.phone_lbl.text];

    cell.web_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"website"] objectAtIndex:indexPath.row]];
    //[websiteary addObject:cell.web_lbl.text];

    cell.sens_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"member_since"] objectAtIndex:indexPath.row]];
    //[sensary addObject:cell.sens_lbl.text];

    cell.des_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"des"] objectAtIndex:indexPath.row]];
    //[desary addObject:cell.des_lbl.text];

    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"img_url"] objectAtIndex:indexPath.row]]]];

    UIImage* image = [[UIImage alloc] initWithData:imageData];

    cell.image_view.image =image;
    //[images addObject:cell.image_view.image];

    return  cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];

    [self.tabel_view reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showRecipeDetail"])
    {
        NSIndexPath *indexPath = [self.tabel_view indexPathForSelectedRow];
        member_details *destViewController = segue.destinationViewController;
        destViewController.hello = [titles objectAtIndex:indexPath.row];

    }
}

最佳答案

因为您已经直接从 tableview 加入了 segue,所以这样做不是正确的方法。而不是你必须通过 Controller 加入 segue 到 Controller 。因此,在获得正确的选定行索引之前,您的 View Controller 快速推送并且您始终获得 0 索引。

您可以在 UITablViewdidSelect 方法中执行相同的操作,而不是在 prepareForSegue 中编写逻辑。

为此,您必须从 Storyboard 中找到目标 View Controller 。并将数据设置为所需的属性。

编辑

删除您的准备安全逻辑并在 didSelect 中执行此操作

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     [arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];

     [self.tabel_view reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];

     UIStoryboard *storyboard = 
        [UIStoryboard storyboardWithName:@"MainStoryboard" 
                                  bundle:[NSBundle mainBundle]];
     YourDestinationVC *vc =[[storyboard instantiateViewControllerWithIdentifier:@"YourDestinationVC"]; //@"YourDestinationVC" is storyboardID for your destination view controller
     vc.hello = [titles objectAtIndex:indexPath.row];
     [[self navigationController] pushViewController:vc animated:YES];
}

截图如下:

enter image description here

关于ios - 如何在选择的 UITableViewCell 行中传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38259542/

相关文章:

ios - 添加 UI 动画

ios - UIViewControllers 在 UIPageViewController 中被重用

iphone - 在 iPad 设备上测试应用程序

ios - 在“id<NSCopy>”类型的对象上找不到读取字典元素的预期方法

objective-c - 保持另一个应用程序的窗口处于事件状态,同时仍然从另一个应用程序的窗口接收鼠标事件?

ios - ios 7中的UINavigationbar后退按钮

iPhone SDK 二维整数数组

ios - insertRowsatIndexPath 异常 iOS

c++ - 从 Objective-C 调用 C++

iOS 获取基于区域设置的号码描述