Ios 为UITableView重新加载数据不工作;tableView在记录时返回NULL

Ios 为UITableView重新加载数据不工作;tableView在记录时返回NULL,ios,uitableview,null,reloaddata,Ios,Uitableview,Null,Reloaddata,我正在从另一个类调用TableViewController类中的方法 要调用显示tableview的方法,请执行以下操作: TableViewController *tableVC = [[TableViewController alloc]init]; [tableVC setTableViewContent]; 然后在TableViewController.h中 @interface TableViewController : UITableViewController <UITab

我正在从另一个类调用TableViewController类中的方法

要调用显示tableview的方法,请执行以下操作:

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:UITableViewController
{
NSMutableArray*nameArray;
}
-(void)setTableViewContent;
@属性(非原子,强)IBUITableView*tableView;
@结束
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;
}
@实现TableViewController
@综合桌面视图;
-(无效)viewDidLoad
{ 
nameArray=[[NSMutableArray alloc]init];
[超级视图下载];
}
-(无效)setTableViewContent{
AppDelegate*AppDelegate=(AppDelegate*)[[UIApplication sharedApplication]委托];
对于(int i=0;i<[appDelegate.businessArray count];i++)
{
NSDictionary*businessDict=[[appDelegate.businessArray对象索引:i]valueForKey:@“位置”];
nameArray=[appDelegate.businessArray valueForKey:@“name”];
}
NSLog(@“%@”,nameArray);
NSLog(@“tableview:%@”,tableview);
//这里tableview返回null
[tableView重新加载数据];
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
//返回节数。
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
//返回节中的行数。
返回[nameArray count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@“正在更新tableview…”);
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//配置单元格。。。
cell.textlab.text=[nameArray objectAtIndex:indexath.row];
返回单元;
}
由于某种原因,当我尝试记录tableview时,它返回null,因此重载数据不起作用。委托和数据源在IB中正确连接,并且tableView有一个引用出口


知道这是怎么回事吗?提前感谢

发生这种情况是因为您在tableView实际存在之前调用了该方法。简单地初始化该类不会绘制表本身,因此在实际创建表之前使用重载数据实际上没有任何意义

在这种情况下,您要做的是在调用setTableViewContent的任何类中创建nameArray,然后通过自定义init方法或在加载该表视图控制器之前设置tableVC.nameArray将其传入

我要做的是创建像
-(id)initWithArray:(NSMutableArray*)nameArr这样的自定义init方法

应该是这样的:

if (self = [super init]) {
    nameArray = [nameArr copy];
}
return self;
然后是
TableViewController*tableVC=[[TableViewController alloc]init]
put
TableViewController*tableVC=[[TableViewController alloc]initWithArray:theNameArray]其中,NameArray是setTableViewContent中的内容(您现在在调用表视图的同一个类中生成,而不是在表视图本身中生成)


有意义吗?

如果将表视图控制器添加到容器视图,则可以在prepareForSegue中获取对该控制器的引用。对于容器视图中的控制器,prepareForSegue将在父控制器的viewDidLoad之前调用,因此无需执行任何操作即可调用它。在下面的示例中,我将segue称为“TableEmbed”——您需要在IB中为segue提供该标识符

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"TableEmbed"]) {
        TableViewController *tableVC = (TableViewController *)segue.destinationViewController;
        [tableVC setTableViewContent];
    }
}
请注意,在调用任一控制器的viewDidLoad之前会调用prepareForSegue:sender:,因此您应该将数组的初始化移动到setTableViewContent,并且您的重新加载表应该进入viewDidLoad


顺便说一句,我不清楚为什么要从另一个类调用setTableContent。为什么不将该方法中的所有代码移到表视图控制器的viewDidLoad方法中?

我通过在
UITableViewController上创建一个“安全”的重新加载方法解决了类似的情况:

- (void)reloadTableViewData
{
    if ([self isViewLoaded])
        [self.tableView reloadData];
}
根据报告:

调用此方法将报告是否加载了视图。与view属性不同,如果视图不在内存中,它不会尝试加载该视图


因此,随时在table view控制器上调用
reloadTableViewData
是安全的。

[tableView reloadData]
之后您是否尝试过它?@AhmedZ。是的,仍然返回null您需要检查类中是否存在
-(id)initWithStyle:(UITableViewStyle)style{self=[super initWithStyle:style];if(self){}return self;}
。@AhmedZ。我添加了这一点,但它并没有修复它。您是如何在屏幕上获得表视图控制器的?我怀疑这行,TableViewController*tableVC=[[TableViewController alloc]init];,正在实例化控制器的新实例,而不是屏幕上的实例。我理解您的逻辑,但当我将其实现到tableView中时,效果并不理想。我使用
TableViewController*tableVC=[[tableviewcontrolleralloc]initWithArray:nameArray]调用init然后在tableviewcontroller中放入
-(id)initWithArray:(NSMutableArray*)nameArr{if(self=[super init]){namearry=[nameArr copy];}return self;}
和nameArray返回良好,但没有生成tableview。此外,在调用它之前,tableview确实存在。空的tableView是应用程序中的第一个屏幕,我可以手动填充它。如果在viewDidLoad底部运行[tableView reloadData],会发生什么情况?可能那里发生了什么奇怪的事情。另外,由于您现在正在init中设置nameArray,请确保删除
nameArray=[[NSMutableArray alloc]init]从视图下载!哎呀。我删除了alloc init并在viewDidLoad中添加了重载数据。仍然没有调用任何东西来设置TableView最后一个想法,因为我