如何从表视图iOS向另一个页面发送值

时间:2014-12-11 09:22:48

标签: ios

我已经完成了从JSON解析数据并将其显示在Table视图中。 所以我想将数据(catID和catName)传递给另一个Page它是DetailPageController 我怎么能这样做?以下是我的代码。 提前谢谢!

#import "ViewController.h"

@interface ViewController ()
{
    NSMutableArray *myObject;
    // A dictionary object
    NSDictionary *dictionary;
    // Define keys
    NSString *catName;
    NSString *catID;



}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    catName =@"Category Name";
    catID=@"Category ID";

    myObject = [[NSMutableArray alloc] init];

    NSData *jsonSource = [NSData dataWithContentsOfURL:
                          [NSURL URLWithString:@"MY_URL"]];

    id jsonObjects = [NSJSONSerialization JSONObjectWithData:
                      jsonSource options:NSJSONReadingMutableContainers error:nil];

    for (NSDictionary *dataDict in jsonObjects) {
        NSString *TheCatName = [dataDict objectForKey:@"cat_name"];
        NSString *TheCatID = [dataDict objectForKey:@"cat_id"];


        NSLog(@"cat_name: %@",TheCatName);
        NSLog(@"cat_id: %@",TheCatID);



        dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                      TheCatName, catName,
                      TheCatID, catID,

                      nil];
        [myObject addObject:dictionary];
    }



}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return myObject.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Item";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell=[[UITableViewCell alloc]initWithStyle:
              UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }


    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];

    NSMutableString *textcat;

            textcat = [NSMutableString stringWithFormat:@"%@",
            [tmpDict objectForKeyedSubscript:catName]];

    NSMutableString *catid;
    catid = [NSMutableString stringWithFormat:@"ID: %@ ",
              [tmpDict objectForKey:catID]];

    cell.textLabel.text = textcat;
    cell.detailTextLabel.text= catid;

    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

didSelectRowAtIndexPath方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];


    NSMutableString *catID;
    catID = [NSMutableString stringWithFormat:@"%@",
             [tmpDict objectForKeyedSubscript:cat_id]];


    NSLog(@"didSelectRowAtIndexPath");
    /*UIAlertView *messageAlert = [[UIAlertView alloc]
     initWithTitle:@"Row Selected" message:@"You've selected a row" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];*/
    UIAlertView *messageAlert = [[UIAlertView alloc]
                                 initWithTitle:@"Row Selected" message:catID delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    // Display the Hello World Message
    [messageAlert show];

    // Checked the selected row
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    [tableView deselectRowAtIndexPath:indexPath animated:YES];



   }

1 个答案:

答案 0 :(得分:0)

首先在DetailPageController类中导入TableviewController,并提供以下行的帮助。

#import "DetailPageController.h"

现在在TableViewController Class

中实施以下方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailPageController * detailPageControllerOBJ_ = [[ImageOptionViewController alloc]initWithNibName:@“XIBName bundle:nil];
    detailPageControllerOBJ_.dicSelectedData = [self.arrayData objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailPageControllerOBJ_ animated:YES];
}

并在DetailPageController.h中放入以下代码。

@property (nonatomic,strong) NSDictionary * dicSelectedData;

您将在dicSelectedData Dictionary

中获取所选数据