iphone nsurlconnect、表格视图和活动指示器

发布于 2024-10-05 04:54:52 字数 5878 浏览 3 评论 0原文

我有一个方法可以执行连接来检索一些数据并填充表视图。 这个方法效果很好。 现在我在 viewDidLoad 中启动这个方法,并

[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];
}

我在 viewDidLoad 中初始化了这个)

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

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

哪里是启动和停止我的 ActivityIndi​​catorView 的更好位置?

我尝试从

[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 =@".+class=\"dettaglioSoglie.*";
 NSString *reg3 =@"</table>";
NSString*reg4=@"<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 开始和停止,但数据未填充在我的表中(我没有看到空表,我没有看到任何表)。 如果我不执行动画,我会得到正确的数据表。 谢谢。

i've a method that perform a connection to retreive some data and popolate a tableview.
This method works great.
Now i'm launching this method in viewDidLoad with

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

i've create this other function:

- (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];
}

(i've initialite this in viewDidLoad)

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

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

where's the better place to start and stop my activityindicatorview?

I've try to start with

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

Here's my pretty standard code for table datasource:

- (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;
}

this is the method for get my data:

- (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 =@".+class=\"dettaglioSoglie.*";
 NSString *reg3 =@"</table>";
NSString*reg4=@"<td><b>(.+) </b></td><td>(.+)  </td><td>(.+)  </td><td>(.+)  </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];
}   

and this is how i've configure my spinning:

- (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];
}

with no lucky: jobs were perform, i see with nslog my data, av start and stop but data were not populated in my table (i don't see empty table, i don't see any table).
if i don't perform my animation i get my right table with data.
Thank's.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知在何时 2024-10-12 04:54:52

我不知道这是正确的方法,但这对我来说效果很好。

-(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。

I don't know this is the correct way or not but this works fine for me.

-(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];
}

Any Function called by thread should have a NSAutoReleasePool as per documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文