[英]Delay table view loading or load array before viewDidLoad?
我正在使用Web服務填充項目數組,然后將其用於填充表格視圖的單元格。
現在我在表視圖本身有問題。 當我檢查“ numberofrows”方法時,該數組尚未加載。 由於某種原因,它會“緊隨其后”加載(根據我的nslog和斷點),即使我已盡早在該控制器中放置了每種加載方法。
現在我不知道的是:
我試過的:將tableview reloadData。 但這根本行不通。 由於某種原因,編譯器會讀取該行,但不會加載任何新內容,即使此時數組已滿。
我想念什么嗎?
(如果將硬編碼的對象放在數組中,我的表視圖就可以正常工作)
我可以編輯並添加一些您要求的代碼,但是由於這看起來像是學校的問題,也許我只是忘記了一些東西。
伙計們,我都很高興! 編輯:
我不同的方法; 我刪除了不必要的代碼,以便閱讀。 編譯器從不進入cellForRow,因為numberOfRows返回零。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CustomCellTableViewCell";
CustomCellTableViewCell *cell = [self.tbUpcomingEvents dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
cell.lbTitle = [_eventList objectAtIndex:indexPath.row];
return cell;
}
行數; nslog返回零。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"numOfRows : %i", [_eventList count]);
return [_eventList count];
}
我的webservice方法,這是從“ webservice類”中調用的方法,用於加載數據。 在這里,NSLog顯示了完整的陣列。
- (void)loadData:(NSMutableArray*)arrayEvent
{
//arrayEvent is full from the internet data. eventList is also full on the NSLog.
_eventList = arrayEvent;
NSLog(@"LoadData : %i", [_eventList count]);
[self.tbUpcomingEvents reloadData];
}
我的viewdidLoad方法
- (void)viewDidLoad
{
[super viewDidLoad];
//Calling my UseCaseController to load the data from the internet.
UCC *sharedUCC = [UCC sharedUCC];
[sharedUCC getUserEventsInDbDis:self :_user];
}
如您所說,您正在使用Web服務。 因此它正在塊中執行您的代碼,這意味着它在單獨的線程中運行。 現在,按照Apple文檔的規定,UI更新應該在主線程中進行。 因此,對於表創建,您需要在下面的GCD代碼中包括以下內容:
dispatch_async(dispatch_get_main_queue,()^{
// write your code for table creation here
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.