我有一个UIProgressBar,它在HTML Parsing for循环中更新。但它没有顺利更新。我希望它能够顺利增长。我试图输入async_dispatch但它根本没有更新。这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelectorOnMainThread:@selector(getWeeklyProgram) withObject:nil waitUntilDone:NO];
}
-(void)getWeeklyProgram
{
NSURL *programURL = [NSURL URLWithString:@"http://www.mostlifeclub.com/studyo-programlari/"];
NSData *programHtmlData = [NSData dataWithContentsOfURL:programURL];
TFHpple *programHTMLParser = [TFHpple hppleWithHTMLData:programHtmlData];
NSString *activitiewXpathQueryString = @"//div[@class='wpb_row vc_row-fluid prk_full_width prk_section parallaxed']/div[@class='prk_inner_block columns centered']/div[@class='vc_span12 wpb_column column_container']/div[@class='wpb_wrapper']/div[@class='wpb_tabs wpb_content_element']//div[@class='wpb_wrapper wpb_tour_tabs_wrapper ui-tabs vc_clearfix']/div[@class='wpb_tab ui-tabs-panel wpb_ui-tabs-hide vc_clearfix']";
NSArray *activityNodes = [programHTMLParser searchWithXPathQuery:activitiewXpathQueryString];
NSMutableArray *activities = [[NSMutableArray alloc] init];
//for each node
for (TFHppleElement *element in activityNodes)
{
currentProgressCount += 1;
@try
{
//Here I want to increase ProgressBar
[self increaseProgress:currentProgressCount :activityNodes.count];
}
@catch(NSException* ex)
{
}
}
}
-(void)increaseProgress:(int)currentLine:(int)totalLines {
progressView.progress = (CGFloat)currentLine / (CGFloat)totalLines;
if(progressView.progress >= 1.0f)
{
NSLog(@"bitti");
}
}
答案 0 :(得分:0)
在另一个Thread中运行For循环和时间密集型东西,并在主线程中更新UI:
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Time Intensive Code
dispatch_sync(dispatch_get_main_queue(), ^{
// Update UI
});
});
您可能需要将dispatch_sync...
放在for循环中以更新UI