[英]objective c table view cell drawn before data source
The data source for my UITableview cells is in: 我的UITableview单元格的数据源在:
- (void)connectionDidFinishLoading:(NSURLConnection *)conn {
// We are just checking to make sure we are getting the XML
NSString *xmlCheck = [[[NSString alloc] initWithData:xmlData encoding:NSWindowsCP1252StringEncoding] autorelease];
// NSLog(@"xmlCheck2 = %@", xmlCheck);
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:xmlData];
for (int i=2; i<33; i++) {
NSString *link=[NSString stringWithFormat:@"/html/body/table/tr[%d]/td",i];
NSArray *elements = [xpathParser searchWithXPathQuery:link];
NSString *date = [NSString stringWithFormat:@"%@, %@",[elements[5]text],[elements[1]text]];
[times addObject:date];
[names addObject:[elements[2]text]];
[types addObject:[elements[3]text]];
[places addObject:[elements[4]text]];
NSLog(@"%@", elements);
NSLog(@"%@", [elements[0] text]);
}
}
But the method that draws the cell is called before the connection is finished even though the connection is started before I draw the cells. 但是,即使在我绘制单元格之前就已开始连接,绘制单元格的方法还是在连接完成之前被调用。 How do I delay the draw cell method or make sure the connection is finished before I draw the cells?
如何在绘制单元格之前延迟绘制单元格方法或确保连接完成?
You need to set tableview's datasource & delegate properties only after you finish loading data. 仅在完成加载数据之后,才需要设置tableview的数据源和委托属性。 If you have set delegate & datasource of tableview from IB or Storyboard, remove it.
如果您已经从IB或Storyboard设置了tableview的委托和数据源,请将其删除。 Set delegate & datasource properties of tableview after you finish loading data.
完成加载数据后,设置tableview的委托和数据源属性。 And reload table as well.
并重新加载表。
- (void)connectionDidFinishLoading:(NSURLConnection *)conn {
// We are just checking to make sure we are getting the XML
NSString *xmlCheck = [[[NSString alloc] initWithData:xmlData encoding:NSWindowsCP1252StringEncoding] autorelease];
// NSLog(@"xmlCheck2 = %@", xmlCheck);
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:xmlData];
for (int i=2; i<33; i++) {
NSString *link=[NSString stringWithFormat:@"/html/body/table/tr[%d]/td",i];
NSArray *elements = [xpathParser searchWithXPathQuery:link];
NSString *date = [NSString stringWithFormat:@"%@, %@",[elements[5]text],[elements[1]text]];
[times addObject:date];
[names addObject:[elements[2]text]];
[types addObject:[elements[3]text]];
[places addObject:[elements[4]text]];
NSLog(@"%@", elements);
NSLog(@"%@", [elements[0] text]);
}
[tableView setDelegate:self];// set delegate, datasource & reload data.
[tableView setDatasource:self];
[tableView reloadData];
}
that's the point of ASYNCHRONOUS networking :) Your main thread doesnt wait for it to finish! 这就是异步网络的关键:)您的主线程不等待它完成! synchronous is there but it's bad
同步在那里,但是很糟糕
Have your UI handle the case that data isnt available yet. 让您的UI处理尚未提供数据的情况。
a) set tableview hidden, show a spinning wheel and show and reload the table when connectionDidFinish is called a)将tableview设置为隐藏,显示一个纺车,并在调用connectionDidFinish时显示并重新加载表
mock code 模拟代码
-viewWillAppear {
table.hidden = YES;
spinningActivity.hidden = NO;
networkConnection start];
}
-connectionDidFinish {
spinningActivity.hidden = YES;
[table reloadData];
table.hidden = NO;
}
The method that provides cell information is only called if you tell it there are rows ready to display. 提供单元格信息的方法只有在您告诉它有准备显示的行时才被调用。 If you tell it the right thing in
tableView:numberOfRowsInSection:
-- which could be 0 if the connection hasn't finished -- there shouldn't be any incorrect calls to tableView:cellForRowAtIndexPath:
. 如果您在
tableView:numberOfRowsInSection:
告诉它正确的事情-如果连接未完成,则可能为0-不应对tableView:cellForRowAtIndexPath:
进行任何不正确的调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.