別のクラスから TableViewController クラスのメソッドを呼び出しています。
テーブルビューを表示するメソッドを呼び出すには、次のようにします。
TableViewController *tableVC = [[TableViewController alloc]init];
[tableVC setTableViewContent];
次にTableViewController.hで
@interface TableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *nameArray;
}
-(void)setTableViewContent;
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@end
TableViewController.m
@implementation TableViewController
@synthesize tableView;
- (void)viewDidLoad
{
nameArray = [[NSMutableArray alloc]init];
[super viewDidLoad];
}
-(void)setTableViewContent{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
for(int i=0;i< [appDelegate.businessArray count];i++)
{
NSDictionary *businessDict = [[appDelegate.businessArray objectAtIndex:i] valueForKey:@"location"];
nameArray = [appDelegate.businessArray valueForKey:@"name"];
}
NSLog(@"%@", nameArray);
NSLog(@"tableview: %@", tableView);
// here tableview returns null
[tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [nameArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"updating tableview...");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = [nameArray objectAtIndex:indexPath.row];
return cell;
}
テーブルビューをログに記録しようとすると、何らかの理由で null が返されるため、ReloadData が機能しません。デリゲートとデータソースは IB で適切に接続されており、tableView の参照アウトレットがあります。
ここで何が起こっているのか分かりますか?前もって感謝します