在目标c ios中保留viewDidLoad之外的数据

在目标c ios中保留viewDidLoad之外的数据,ios,objective-c,arrays,uitableview,viewdidload,Ios,Objective C,Arrays,Uitableview,Viewdidload,我知道以前有人问过这个问题,但提供的答案并没有解决我的问题 例如,我在viewDidLoad中列出了一个非常简单的对象数组: @implementation MyViewController { NSArray *tableData; } 在表视图中使用cellForRowAtIndexPath调用 cell.nameLabel.text = [tableData objectAtIndex:indexPath.row]; 这很好,NSLog显示了我的阵列。但是,当我在viewDidLoa

我知道以前有人问过这个问题,但提供的答案并没有解决我的问题

例如,我在viewDidLoad中列出了一个非常简单的对象数组:

@implementation MyViewController {
NSArray *tableData;
}

在表视图中使用cellForRowAtIndexPath调用

cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
这很好,NSLog显示了我的阵列。但是,当我在viewDidLoad之外概述tableData时,我的数组是(null)


我的问题是,当数组在ViewDidLoad之外指定时,如何使其可用于tableView

编辑:以下是我的具体代码:

#import <UIKit/UIKit.h>
#import "PhotoView.h"
@interface FrontViewController : UIViewController

@property (nonatomic, retain) UITableView *tableView;

@end
#导入
#导入“PhotoView.h”
@界面FrontViewController:UIViewController
@属性(非原子,保留)UITableView*tableView;
@结束

#导入“FrontViewController.h”
#导入“StreamScreen.h”
#导入“API.h”
#导入“PhotoView.h”
#导入“StreamPhotoScreen.h”
#导入“PrivateViewController.h”
#导入“SWRevealViewController.h”
#导入“PhotoScreen.h”
#导入“RearViewController.h”
#导入“SimpleTableCell.h”
@接口FrontViewController()
//私人方法:
-(iAction)pushExample:(id)发送方;
@结束
@FrontViewController的实现{
NSArray*表格数据;
}
#pragma标记-视图生命周期
-(无效)viewDidLoad
{
[超级视图下载];
self.title=NSLocalizedString(@“前视图”,无);
SWRevealViewController*revealController=[self revealViewController];
[self.navigationController.navigationBar addgestureRecognitor:revealController.PangestureRecognitor];
UIBarButtonItem*RevealButtonitItem=[[UIBarButtonItem alloc]initWithImage:[UIImage ImageName:@“reveal icon.png”]
样式:UIBarButtonimStyleBordeded目标:revealController操作:@selector(revealToggle:)];
self.navigationItem.leftBarButtonItem=revealButtonItem;
//如果我取消注释,这会起作用
//tableData=[NSArray arrayWithObjects:@“您好”,“我的”,“我的名字”,“是”,无];
[自我刷新流];
}
-(无效)刷新流{
//从web API调用“stream”命令
[[API sharedInstance]命令和参数:
[NSMutableDictionary dictionaryWithObjectsAndKeys:@“stream”@“command”,nil]
onCompletion:^(NSDictionary*json){
//得到流
[self-showStream:[json objectForKey:@“结果”];
NSMutableArray*myData=[[NSMutableArray alloc]init];
myData=[json objectForKey:@“结果”];
NSArray*userNameData=[myData valueForKey:@“username”];
[自动加载数据];
tableData=用户名数据;
[self.tableView重载数据];
//我可以在NSLog中看到我的json数组
NSLog(@“结果如下:%@”,tableData);
}];
}
//这也不行
//-(void)加载数据{
//将数据添加到数组中。
//tableData=[NSArray arrayWithObjects:@“您好”,“我的”,“我的名字”,“是”,无];
//NSLog(@“我的数据:%@”,tableData);
//现在加载表视图。
//[self.tableView重载数据];
//}
-(void)showStream:(NSArray*)流{

对于(int i=0;iOk),我对您的问题的理解是,您希望用另一种方法(而不是viewDidLoad)为NSArray分配变量,然后加载表视图

这很简单,只需创建一个方法,负责将数据添加到数组中,然后重新加载表视图,如下所示:

-(void)viewDidLoad {
    [super viewDidLoad];

    // Call your method.
    [self loadData];
}

-(void)loadData {

    // Add the data to your array.
    tableData = [NSArray arrayWithObjects:@"Hello", @"My", @"Name", @"Is"];
    NSLog(@"My Data: %@", tableData);

    // Now load the table view.
    [myTableView reloadData];
}
更新1

如果你能和我们分享你的代码,那会更有帮助。你的工作是如何进行的,如何设置你的tableview,何时调用它/等等

更新2

好吧,看来你的问题很明显。你的表视图永远不会那样加载。你需要在
cellForRowatineXpath
方法之外调用tableview
reloadData
方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    SimpleTableCell *cell = (SimpleTableCell *)[self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
    cell.prepTimeLabel.text = [prepTime objectAtIndex:indexPath.row];

    return cell;   
}
因此,方法调用
[self.tableView reloadData];
应该只在
刷新流
方法中调用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    SimpleTableCell *cell = (SimpleTableCell *)[self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
    cell.prepTimeLabel.text = [prepTime objectAtIndex:indexPath.row];

    return cell;   
}
更新3

在向NSMutableArray添加数据之前,需要初始化NSMutableArray。在
refreshStrem
方法中初始化它,如下所示:

-(void)refreshStream {

    // call the "stream" command from the web API
    [[API sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"stream", @"command", nil] onCompletion:^(NSDictionary *json) {
    //got stream

        [self showStream:[json objectForKey:@"result"]];

        NSMutableArray *myData = [[NSMutableArray alloc] init];
        myData = [json objectForKey:@"result"];

        NSArray *userNameData = [myData valueForKey:@"username"];
    }];

    tableData = userNameData;
    [self.tableView reloadData];
}
更新4

好的,在阅读了@jrturton答案之后,我认为我的答案是垃圾是安全的。任何阅读我答案的人,请查看@jrturton帖子

-(void)refreshStream {
    // call the "stream" command from the web API
    [[API sharedInstance] commandWithParams:
        [NSMutableDictionary dictionaryWithObjectsAndKeys:@"stream", @"command", nil] 
        onCompletion:^(NSDictionary *json) {
            //got stream

            [self showStream:[json objectForKey:@"result"]];
            NSMutableArray *myData = [json objectForKey:@"result"];
            NSArray *userNameData = [myData valueForKey:@"username"];
    }];

    tableData = userNameData;

    [self.tableView reloadData]; 
}
在这里,异步编程陷入了一个非常常见的陷阱

commandWithParams
接受一个完成块,这是从JSON中获取数据的地方。在API调用返回之前,不会执行此块。运行此代码时发生的事件顺序为:

  • 调用
    commandWithParams
  • tableData
    被分配给
    userNameData
    的内容(您可能已经在其他地方声明过了,否则这甚至无法编译)
  • 调用
  • 重新加载数据
  • ……时间流逝
  • 执行完成块,将JSON读入局部变量,然后立即销毁

  • 您需要将两行(上面列表中的第2点和第3点)移到完成块内。在块返回之前,您的表将没有数据。

    我觉得很不好意思。答案很简单。我对Json和API非常着迷,以至于没有检查基本信息。我只需要在.h文件中:

    @property (strong, nonatomic) IBOutlet UITableView *tableView;
    
    我最初有:

    @property (nonatomic, retain) UITableView *tableView;
    

    因此,您基本上是在问如何在viewDidLoad之外为数组分配变量/等?如果是这样,只需创建自己的方法并使用它。“当它在viewDidLoad之外指定时”,是否希望在整个应用程序中全局访问数据。如果tableview未在requ中显示此值,则此数据也将在外部可用
    @property (nonatomic, retain) UITableView *tableView;