3

別のクラスから 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 の参照アウトレットがあります。

ここで何が起こっているのか分かりますか?前もって感謝します

4

3 に答える 3

0

で「安全な」リロード メソッドを作成することで、同様の状況を解決しましたUITableViewController

- (void)reloadTableViewData
{
    if ([self isViewLoaded])
        [self.tableView reloadData];
}

のドキュメントにisViewLoadedよると:

このメソッドを呼び出すと、ビューが読み込まれているかどうかが報告されます。ビュー プロパティとは異なり、ビューがまだメモリにない場合、ビューをロードしようとしません。

したがってreloadTableViewData、いつでも安全に Table View Controller を呼び出すことができます。

于 2014-01-10T04:49:41.893 に答える