【发布时间】:2015-05-01 07:33:41
【问题描述】:
我知道之前有人问过这个问题,但提供的答案并没有解决我的问题。
例如,我在 viewDidLoad 中有一个非常简单的对象数组:
@implementation MyViewController {
NSArray *tableData;
}
- (void)viewDidLoad
{
[super viewDidLoad];
tableData = [NSArray arrayWithObjects:@"Hello", @"My", @"Name", @"Is"];
NSLog(@"My Data: %@", tableData);
在 tableView 中使用 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
#import "FrontViewController.h"
#import "StreamScreen.h"
#import "API.h"
#import "PhotoView.h"
#import "StreamPhotoScreen.h"
#import "PrivateViewController.h"
#import "SWRevealViewController.h"
#import "PhotoScreen.h"
#import "RearViewController.h"
#import "SimpleTableCell.h"
@interface FrontViewController()
// Private Methods:
- (IBAction)pushExample:(id)sender;
@end
@implementation FrontViewController{
NSArray *tableData;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(@"Front View", nil);
SWRevealViewController *revealController = [self revealViewController];
[self.navigationController.navigationBar addGestureRecognizer:revealController.panGestureRecognizer];
UIBarButtonItem *revealButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reveal-icon.png"]
style:UIBarButtonItemStyleBordered target:revealController action:@selector(revealToggle:)];
self.navigationItem.leftBarButtonItem = revealButtonItem;
// This works if I uncomment
//tableData = [NSArray arrayWithObjects:@"Hello", @"My", @"Name", @"Is", nil];
[self refreshStream];
}
-(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"];
[self loadData];
tableData = userNameData;
[self.tableView reloadData];
// I can see my json array in NSLog
NSLog(@"here's the results: %@", tableData);
}];
}
//This doesn't work either
//-(void)loadData {
// Add the data to your array.
//tableData = [NSArray arrayWithObjects:@"Hello", @"My", @"Name", @"Is", nil];
//NSLog(@"My Data: %@", tableData);
// Now load the table view.
// [self.tableView reloadData];
//}
-(void)showStream:(NSArray*)stream {
for (int i=0;i<[stream count];i++) {
NSDictionary* photo = [stream objectAtIndex:i];
}
NSArray *checkData = [stream valueForKey:@"username"];
//I can see my data in NSLog
NSLog(@"here's the results: %@", checkData);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 78;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[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;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
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:[tableData objectAtIndex:indexPath.row] 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];
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"willSelectRowAtIndexPath");
if (indexPath.row == 0) {
return nil;
}
return indexPath;
}
@end
【问题讨论】:
-
所以您基本上是在问如何将变量/等分配给 viewDidLoad 之外的数组?如果是这样的话,只需制作自己的方法并使用它。
-
"当它在 ViewDidLoad 之外指定时",您是否要在整个应用程序中全局访问数据。
-
这个数据也可以在外面使用,如果你的tableview没有用需要的单元格显示这个值,你可能在
viewDidLoad方法初始化后忘记了UITableview的reloadData。跨度> -
请显示数组为
(null)的代码 -
我已经编辑了我的问题以显示我的确切代码。谢谢。
标签: ios objective-c arrays uitableview viewdidload