2010-11-25 53 views
1

我有一個方法執行連接來檢索一些數據和popolate tableview。 這種方法效果很好。 現在我發動viewDidLoad中此方法iphone nsurlconnect,tableview和活動指標

[NSThread detachNewThreadSelector:@selector(connTre) 
toTarget:self 
withObject:nil];  

我創建這個其他功能:

- (void)initSpinner { 
av = [[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(195.0, 8.0, 30.0, 30.0) ] autorelease];  
av.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; 
[av hidesWhenStopped]; 
[self.view addSubview:av]; 
} 

(我initialite這viewDidLoad中)

- (void)spinBegin { 
    [av startAnimating]; 
} 

- (void)spinEnd { 
[av stopAnimating]; 
} 

在哪裏開始和停止我的activityindicatorview更好的地方?

我嘗試啓動

[self performSelectorOnMainThread:@selector(spinBegin) 
withObject:nil 
waitUntilDone:false]; 

這裏是我的表數據源非常標準代碼:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return [listaOggetti count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
NSDictionary *dictionary = [listaOggetti objectAtIndex:section]; 
NSArray *array = [dictionary objectForKey:@"Elementi"]; 
return [array count]; 

} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
return 30; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
reuseIdentifier:CellIdentifier] autorelease]; 
    cell.selectionStyle = UITableViewCellStyleValue1 ; 
} 
NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]]; 
NSInteger row = [indexPath row]; 

// Configure the cell. 

NSDictionary *dictionary = [listaOggetti objectAtIndex:indexPath.section]; 
NSArray *array = [dictionary objectForKey:@"Elementi"]; 
NSString *cellValue = [array objectAtIndex:indexPath.row]; 

if (row == 0){ 
    cell.textLabel.text = cellValue; 
    cell.textAlignment = UITextAlignmentCenter; 
    cell.backgroundColor = [UIColor redColor]; 
    cell.font = [UIFont systemFontOfSize:13]; 
    cell.selectionStyle = UITableViewCellStyleValue1 ; 
} else { 


cell.textLabel.text = cellValue; 
cell.textAlignment = UITextAlignmentCenter; 
cell.backgroundColor = [UIColor whiteColor]; 
cell.selectionStyle = UITableViewCellStyleValue1 ; 
} 

return cell; 
} 

這是得到我的數據的方法:

- (void)connTre { 
NSThread *spinThread=[[NSThread alloc] initWithTarget:self 
selector:@selector(startSpinning) object:nil]; 
[spinThread start]; 

NSError *error; 
NSURLResponse *response; 
NSData *dataReply; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: @"myloginurl"]]; 

[request setHTTPMethod: @"GET"]; 
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
//message tre soglie ok 
path = @"my_url_for_getting_data"; 
url = [NSURL URLWithString:path]; 
NSError *errors; 
htmlString = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&errors]; 

NSString *regexString = @"(?:\r\n|[\n\v\f\r\302\205\\p{Zl}\\p{Zp}])"; 
NSString *reg2 [email protected]".+class=\"dettaglioSoglie.*"; 
NSString *reg3 [email protected]"</table>"; 
NSString*[email protected]"<td><b>(.+)&nbsp;</b></td><td>(.+)&nbsp;&nbsp;</td><td>(.+)&nbsp;&nbsp;</td><td>(.+)&nbsp;&nbsp;</td>"; 
NSString *replaceWithString = @"$1"; 
NSString *replaceWithString1 = @"Effettuato $2"; 
NSString *replaceWithString2 = @"Rimanente $3"; 
NSString *replaceWithString3 = @"Totale $4"; 
if(htmlString){ 
    NSArray *linesArray = [htmlString componentsSeparatedByRegex:regexString]; 
    for(NSString *lineString in linesArray) { 
     if(lineString ==[lineString stringByMatching:reg2]) { print = YES;} 
     if (print == YES) { 
      if(lineString ==[lineString stringByMatching:reg4]) { 
       replace = [lineString stringByReplacingOccurrencesOfRegex:reg4 withString:replaceWithString]; 
       replace1 = [lineString stringByReplacingOccurrencesOfRegex:reg4 withString:replaceWithString1]; 
       replace2 = [lineString stringByReplacingOccurrencesOfRegex:reg4 withString:replaceWithString2]; 
       replace3 = [lineString stringByReplacingOccurrencesOfRegex:reg4 withString:replaceWithString3]; 
//NSLog(@"%@\n%@\n%@\n%@\n",replace, replace1, replace2, replace3); 
//sectionz = [NSMutableArray arrayWithObjects: replace, nil]; 
       //NSMutableArray *voice = [NSMutableArray arrayWithObjects: replace, replace1, replace2, replace3, nil]; 
       NSMutableArray *voice = [NSMutableArray arrayWithObjects: replace, replace1, replace2, replace3, nil]; 
       NSDictionary *detVoice = [NSDictionary dictionaryWithObject:voice forKey:@"Elementi"]; 
       [listaOggetti addObject:detVoice];     
       NSLog(@"%@", listaOggetti); 

} 


//NSLog(@"%@",listaDettaglioOggetti); 
} 
if (lineString ==[lineString stringByMatching:reg3]) { print = NO;} 

} 
} else { 
NSLog(@"Error reading file '%@'", htmlString); 
} 

[av stopAnimating]; 
[spinThread release]; 
} 

這就是我如何配置我的旋轉:

- (void)startSpinning { 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
av = [[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(195.0, 8.0, 30.0, 30.0) ] autorelease];  
av.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; 
[av hidesWhenStopped]; 
[self.view addSubview:av]; 
[av startAnimating]; 
[pool release]; 
} 

不幸運的,工作是表演,我看到的NSLog我的數據,AV啓動和停止,但數據在我的表沒有填充(我沒有看到空表,我沒有看到任何表)。 如果我不執行我的動畫我得到我的右表與數據。 謝謝。

回答

0

我不知道這是正確的方式或不,但這對我工作正常。

-(void) yourFunctionThatPopulatesTableView 
{ 
    NSThread *spinThread=[[NSThread alloc] initWithTarget:self selector:@selector(startSpinner) object:nil]; 
    [spinThread start]; 

    //Populate TableView 

    //Last Line of he function 
    [av stopAnimating]; 
    [spinThread release]; 
} 

-(void)startSpinner 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    CGRect frame = CGRectMake(195.0, 8.0, 30.0, 30.0); 
    av = [[UIActivityIndicatorView alloc] initWithFrame:frame]; 
    av.activityIndicatorViewStyle=UIActivityIndicatorViewStyleGray; 
    [av hidesWhenStopped]; 
    [self.view addSubview:av]; 
    [av startAnimating]; 
    [pool release]; 
} 

任何由線程調用的函數都應該有一個NSAutoReleasePool按照文檔。

+0

感謝您的回答。我嘗試了你的方式,旋轉開始,我看到我的控制檯互聯網數據,我已經要求,旋轉停止,但我沒有看到我的模擬器上的表。 – zebra 2010-11-25 18:36:00