【问题标题】:Add UIProgressView to a TableView Cell将 UIProgressView 添加到 TableView 单元格
【发布时间】:2012-03-12 23:54:06
【问题描述】:

我有一个应用程序可以显示不同文件的表格视图。我将其设置为异步下载,并希望所选单元格显示进度条。我整个下午都在网上查看,到目前为止,我发现的所有内容似乎都不完整。这是我到目前为止的下载代码,并设置为让进度视图显示下载状态。

代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:entry.articleUrl];    
self.nameit = entry.articleTitle;
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url         cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];   
receivedData = [[NSMutableData alloc] initWithLength:0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self     startImmediately:YES];
}
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
progress.hidden = NO;
[receivedData setLength:0];
expectedBytes = [response expectedContentLength];
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
float progressive = (float)[receivedData length] / (float)expectedBytes;
[progress setProgress:progressive];


}

 - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[connection release];
}

- (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse:    (NSCachedURLResponse *)cachedResponse {
  return nil;
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[currentURL stringByAppendingString:@".mp3"]];
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[receivedData writeToFile:pdfPath atomically:YES];
progress.hidden = YES;
[connection release];
}

【问题讨论】:

  • 您的问题是关于向 tableviewcell 添加进度条,还是关于让进度条显示进度?您显示的代码似乎与标题中的问题不符。
  • 我在询问有关向 tableviewcell 添加进度条的问题。我发布的代码是将开始下载的代码。我发布了一些下载进度条码。我调整了这段代码以在普通视图控制器中运行,在该控制器中我使用 IB 添加了一个进度条,它会下载并显示进度。只是不太清楚如何在下载开始时在选定的表格视图单元格中显示进度条,让它显示进度,然后在下载完成后隐藏。
  • 每个 tableviewcell 都有一个名为 contentView 的视图。只需像添加任何其他视图一样将 progressBar 添加到该 contentView 即可。
  • 好的,我在头文件中创建了一个名为progress的UIProgressView,并为单元格添加:CGRect frame = CGRectMake(0, 49, 160, 50);进度 = [[UIProgressView alloc] initWithFrame:frame]; [cell.contentView addSubview:progress]; didSelect 的代码和所有的 NSURLConnection 代码与 OP 相同。进度条显示,但在每个单元格中,下载开始时不会隐藏,并且根本不会移动。我该怎么做才能让它只出现在选定的单元格中,并且在下载期间一直隐藏?
  • 您在正确的轨道上,但听起来您对面向对象编程的一些基础知识并不清楚。我认为每次向单元格添加一个新的 UIProgressView 实例时,您都会创建一个新的实例,因此您的引用都是不正常的。您需要确保在该特定 tableviewcell 的确切实例上调用您的方法。

标签: iphone asynchronous download tableview uiprogressview


【解决方案1】:

首先,向你的界面添加一个 UIProgressView 变量,这样你就可以从你的类中的任何方法中引用它。

然后,当用户选择它时。你必须初始化它

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];

    progress = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
    progress.frame = CGRectMake(10, 10, 30, 30);
    progress.progress = 0.0;

    progress.center = CGPointMake(23,21);

    [cell.contentView addSubview:progress];


}

现在您可以在文件下载时更新进度条,之后只需发送[progress removeFromSuperView]

就是这样!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    相关资源
    最近更新 更多