简体   繁体   English

如何在viewDidLoad之前初始化UITableView?

[英]How to initialize UITableView before viewDidLoad?

I have to initialize my UITableView instance within viewDidLoad , such as: 我必须在viewDidLoad初始化我的UITableView实例,例如:

tableView1 = UITableView(frame: frame)

However, when I tried to access the view controller that includes the tableView1 , the UITableViewDataSource 's tableView: numberOfRowsInSection: is called before the viewDidLoad method, and within the numberOfRowsInSection: , I want to refer to the tableView1 like if tableView == tableView1 . 但是,当我尝试访问包含tableView1的视图控制器时,在viewDidLoad方法之前以及在numberOfRowsInSection:内调用UITableViewDataSourcetableView: numberOfRowsInSection: viewDidLoad我想引用tableView1就像if tableView == tableView1

However, then how can I initialize the tableView1 before calling viewDidLoad ? 但是,如何在调用viewDidLoad之前初始化tableView1呢? If I tried to initialize it within viewDidLoad but still refer to it from within numberOfRowsInSection: , then it causes the error: fatal error: unexpectedly found nil while unwrapping an Optional value because it is not initialized yet and hence its value is nil . 如果我尝试在viewDidLoad初始化它,但仍从numberOfRowsInSection:引用它,则它将导致以下错误: fatal error: unexpectedly found nil while unwrapping an Optional value因为该值尚未初始化,因此其值为nil

I think if I use storyboard and @IBOutlet , I don't have the problem. 我认为,如果我使用情节@IBOutlet@IBOutlet ,则没有问题。 But now I use them all from my code, so I'm not sure how I can cope with the issue. 但是现在我从代码中全部使用它们,所以不确定如何解决该问题。

UPDATE UPDATE

If this cannot be done as posted by @coolcracker, then how can I port this Objective-C code? 如果不能通过@coolcracker发布此操作,那么如何移植此Objective-C代码?

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo;
if (tableView == _tableView1) {
    sectionInfo = _frc1.sections[section];
} else if (tableView == _tableView2) {
    sectionInfo = _frc2.sections[section];
}

return sectionInfo.numberOfObjects;
}

Don't use Storyboard at all, just declare and initialise your tableview as follows 根本不用故事板,只需声明并初始化表视图即可,如下所示

@interface ApplicationsPageGroups : UIViewController
{
  UITableView* tableView1; 
}

- (void)viewDidLoad 
{
   [super viewDidLoad];
   tableView1 = [[UITableView alloc] init];
   tableView1.frame = CGRectMake(10, 95, [UIScreen mainScreen].bounds.size.width-20, [UIScreen mainScreen].bounds.size.height-230);
   tableView1.delegate = self;
   tableView1.dataSource = self;
   tableView1.tableFooterView = [[UIView alloc] init] ;
   [self.view addSubview:tableView1];
   [self getTableData];
 }
 -(void) getTableData
{
   // after you get some array of data to fill the tableview
   [tableView1 reloadData];
}

Now override all the UITableView delegate and dataSource methods. 现在,重写所有UITableView委托和dataSource方法。

simply add 只需添加

[tableView1 reloadData];

which call your DataSource methods call again 哪个再次调用您的DataSource方法

You could override the view controller initWithFrame or initWithNibOrNil method and initialize the tableViews there. 您可以重写视图控制器的initWithFrame或initWithNibOrNil方法,并在那里初始化tableViews。

Also, have you tried initializing the table views BEFORE calling the [super viewDidLoad] method? 另外,您是否曾在调用[super viewDidLoad]方法之前尝试初始化表视图? I have not checked, but it may be possible that the superclass controller is calling the reload method before your initializers, depending on your class hierarchy. 我还没有检查过,但是取决于您的类层次结构,超类控制器可能会在初始化之前调用reload方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM