如何以编程方式将UITableViewCell中的JSON数组传递给新的UIViewController?

时间:2014-03-14 11:24:06

标签: ios objective-c json uitableview

这是我到目前为止所知道的:

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>

@end

FirstViewController.m

#import "FirstViewController.h"
#import "CellDetailView.h"

@interface FirstViewController()
@property (strong, nonatomic) NSMutableArray *myMutableArray;
@end

- (void)viewDidLoad{
  //...
}

//There's a JSON method here that's working just fine populating the UITableViewCells as intended.
//        ...JSON URL STUFF...
//        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
//        NSError *errorJson = nil;
//        NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&errorJson];
        self.coolDict = [dataDictionary objectForKey:@"coolStuff"];
        self.myMutableArray = [self.coolDict objectForKey:@"stuff_1"];
        [self.tableView reloadData];



//UITableViewCell method here, works great!



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

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    CellDetailView *nextViewController = [[CellDetailView alloc] init]; //This is a UIViewController Class I made.
    NSDictionary *someInfo = self.myMutableArray[indexPath.row];  // This is logging the array for each cell, that's cool.
    NSLog(@"%@", someInfo);
    [[self navigationController] pushViewController:nextViewController animated:YES];
}

CellDetailView.h

#import <UIKit/UIKit.h>
#import "FirstViewController.h"

@interface CampaignItemDetails : UIViewController

@end

CellDetailView.m

#import "CellDetailView.h"
#import "FirstViewController.h"

@interface CellDetailView ()
@end

@implementation CellDetailView


- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UILabel *myDetailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
myDetailLabel.text = someInfo[@"title"];
[self.view addSubview:myDetailLabel];  //THIS DOESNT WORK :(
}

@end

如何将myMutableArray放入CellDetailView中,以便我可以使用更多来自数组的JSON数据填充ViewController?

*我知道我的想法是推送到CellDetailView(而从FirstViewController拉出来)。*

我已经看到了如何将数据从视图控制器传递到视图控制器的答案,但它非常抽象,而且我对目标C很新。我也发现了TON的Storyboard和Nibs,但是我我没有使用这些东西。

3 个答案:

答案 0 :(得分:1)

在didSelectRowAtIndexPath

中使用此代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    CellDetailView *nextViewController = [[CellDetailView alloc] init]; //This is a UIViewController Class I made.
    NSDictionary *someInfo = self.myMutableArray[indexPath.row];  // This is logging the array for each cell, that's cool.
    NSLog(@"%@", someInfo);

    nextViewController.info = someInfo; //You should define info property in your next controller
    [[self navigationController] pushViewController:nextViewController animated:YES];
}

您应该在下一个控制器中定义info属性

@interface CellDetailView : UIViewCintroller
@property (nonatomic, strong) NSDictionary *info;
@end

答案 1 :(得分:1)

我很想像这样覆盖initMethod:

 NSDictionary *someInfo = self.myMutableArray[indexPath.row]; 
 CellDetailView *nextViewController = [[CellDetailView alloc] initWithInfo:someInfo];
 [[self navigationController] pushViewController:nextViewController animated:YES];

在您的cellDetailView中,请确保添加init Mehtod:

- (id) initWithInfo:(NSDictionary *) info {

    self = [super init];

    if (self) {

       //Now you can save the info to a private NSDictionary property:

    }

    return self;
}

确保添加:

- (id) initWithInfo:(NSDictionary *) info;

在你的cellDetailView .h。

答案 2 :(得分:0)

您可以在Cell Class中创建DelegateMethod。

在TableViewCell.h文件中

@protocol CellDelegate <NSObject>
-(void)passJSONArray:(JSONArray *)jsonArray;
@end

@property(nonatomic, assign)id<ButtonCellDelegate> delegate;
您的TableViewCell.m文件中的

创建发送JsonArray的方法。

关于JSONArray Getter方法或者你得到数组的方法

你可以调用sendJSONArray() 像

 -(NSArray *)gotMYJSONArray;
 {
    //getJsonArray
    [self sendJSONArray:jsonArray];
    return jsonArray;
 }

-(void)sendJSONArray(JSONArray *)jsonArray
{
   if([_delegate respondsToSelector:@selector(passJSONArray:)])
    {
        [_delegate passJSONArray:jsonArray];
    }
}

OnYour ViewController

-(void)passJSONArray:(JSONArray *)array
{
    self.jsonArray = array;
}