2012-08-10 121 views
1

我想下载使用asinetworkqueue的网址列表,它下载得很好。但是当我滚动uitableview时,我的uiprogressview隐藏起来。UIProgressView滚动uitableview时隐藏

我的意思是如果进度显示在第2行,如果我滚动它,然后隐藏进度视图。然后完成第2行的下载后,它显示第3行的进度视图。

总之,当滚动uitableview时,活动uiprogressview被隐藏。

请帮我解决这个问题。

非常感谢您的时间..

Upadated-实际问题:当下载未完成行(例如行1号),现在如果我滚动tableview中,看到了一行没有1,即使尚未完成下载,progressview也会隐藏。一旦第1行的下载完成,它开始对第2行进行降低编码,现在显示进度视图。

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     self.view.backgroundColor = [UIColor clearColor]; 

     self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.jpg"]]; 

     CGRect tblViewFrame = CGRectMake(7 , 0 , self.view.bounds.size.width - 14, self.view.bounds.size.height - 90); 
     self.tblViewDownload = [[[UITableView alloc]initWithFrame:tblViewFrame style:UITableViewStyleGrouped]autorelease]; 
     self.tblViewDownload.backgroundColor = [UIColor clearColor]; 
     self.tblViewDownload.showsVerticalScrollIndicator = FALSE; 
     self.tblViewDownload.scrollEnabled = YES; 
     self.tblViewDownload.delegate = self; 
     self.tblViewDownload.dataSource = self; 
     [self.view addSubview:self.tblViewDownload]; 

     index = 0; 

     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ 
      [self loadArray]; 
     }); // Do any additional setup after loading the view. 
    } 



    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

    { 
     // Return the number of sections. 
     return 1; 
    } 


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

    { 
     // Return the number of rows in the section. 
     return [self.myUrlArray count]; 
    } 


    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: 
    (NSIndexPath *)indexPath 

    { 
     CGFloat rowHeight = 75.0; 

     return rowHeight; 
    } 

    // Customize the appearance of table view cells. 
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: 
    (NSIndexPath *)indexPath 

    { 
     NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d", indexPath.row]; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     UILabel *lblTitle, *lblAuthor; 


     if (cell == nil) 
     { 
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero]autorelease]; 

      cell.selectionStyle = UITableViewCellSelectionStyleNone; 

      lblTitle = [[[UILabel alloc]initWithFrame:CGRectMake(65, 10, 220, 25)]autorelease]; 
      lblTitle.backgroundColor = [UIColor clearColor]; 
      //lblTitle.font = [UIFont boldSystemFontOfSize:15.0]; 
      lblTitle.font = [UIFont fontWithName:@"Palatino-Bold" size:15.0]; 
      lblTitle.text = @"Sample title"; 
      [cell.contentView addSubview:lblTitle]; 

      lblAuthor = [[[UILabel alloc]initWithFrame:CGRectMake(65, 30, 220, 25)]autorelease]; 
      lblAuthor.backgroundColor = [UIColor clearColor]; 
      lblAuthor.font = [UIFont italicSystemFontOfSize:13.0]; 
      lblAuthor.textColor = [UIColor grayColor]; 
      lblAuthor.text = @"sample authior"; 
      [cell.contentView addSubview:lblAuthor]; 



     } 

     return cell; 
    } 


    -(void)loadArray{ 

     NSString *urk = @"http://sample/iphone/video/video_2.mov"; 
     self.myUrlArray = [[NSMutableArray alloc]init]; 

     for(int i = 0;i<10;i++){ 
     [self.myUrlArray addObject:urk]; 
     } 


     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ 
     [self downLoadPoems]; 
     }); 

    } 

    -(void)downLoadPoems{ 

     if(index < [myUrlArray count]) 
     { 
      NSLog(@"this is the index %d",index); 

     NSIndexPath *myIP = [NSIndexPath indexPathForRow:index inSection:0]; 

     UITableViewCell *Cell = [self.tblViewDownload cellForRowAtIndexPath:myIP]; 

     progress = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar]; 
     progress.frame = CGRectMake(65, 55, 210, 25); 
     progress.progress = 0.0; 
     progress.backgroundColor = [UIColor redColor]; 
     [Cell.contentView addSubview:progress]; 
     } 


     if(!self.networkQueue) 
      self.networkQueue = [[[ASINetworkQueue alloc] init] autorelease]; 

     [self.networkQueue cancelAllOperations]; 
     [self.networkQueue setQueueDidFinishSelector:@selector(queueCompleted:)]; 
     [self.networkQueue setShowAccurateProgress:YES]; 
     [self.networkQueue setDownloadProgressDelegate:progress]; 

     [self.networkQueue setDelegate:self]; 

     if(index < [myUrlArray count]) 
     {   
      NSString *url = [myUrlArray objectAtIndex:index]; 

      NSArray *aPoemArrayUrls = [NSArray arrayWithObjects:url,nil]; 

      for(NSString* urlString in aPoemArrayUrls) 
      { 
       NSURL *url = [NSURL URLWithString:urlString]; 
       ASIHTTPRequest *downloadAFileRequest = [[ASIHTTPRequest requestWithURL:url]retain]; 
       NSString *Filename = [urlString lastPathComponent]; 
       NSLog(@"%@ filename",Filename); 
       [downloadAFileRequest setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]]; 
       [downloadAFileRequest setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:Filename]]; 
       [downloadAFileRequest setShouldContinueWhenAppEntersBackground:YES]; 
       [downloadAFileRequest setDelegate:self]; 
       [downloadAFileRequest setDidFinishSelector:@selector(requestDone:)]; 
       [downloadAFileRequest setDidFailSelector:@selector(requestWentWrong:)]; 
       [downloadAFileRequest setShowAccurateProgress:YES]; 

       [self.networkQueue addOperation:downloadAFileRequest]; 
       //---- 
      } 

      [self.networkQueue go]; 


     } 





    } 

    -(void)queueCompleted:(ASINetworkQueue*)queue{ 

     NSLog(@"queueCompleted"); 

     self.tblViewDownload.userInteractionEnabled = YES; 

     UIProgressView *progressv = (UIProgressView*)queue.downloadProgressDelegate; 

     if (progressv) 
      [progressv removeFromSuperview]; 


     if (index < [myUrlArray count]){ 

      index++; 

      [self downLoadPoems]; 
     } 

    } 


    - (void)requestDone:(ASIHTTPRequest *)request 
    { 
     NSLog(@"request done"); 
     NSString *response = [request responseString]; 
    } 

    - (void)requestWentWrong:(ASIHTTPRequest *)request 
    { 
     NSLog(@"request went wrong"); 

     NSError *error = [request error]; 
    } 

    - (void)viewDidUnload 
    { 
     [super viewDidUnload]; 
     // Release any retained subviews of the main view. 
    } 

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
     return (interfaceOrientation == UIInterfaceOrientationPortrait); 
    } 
+0

你需要澄清这个问题 - 我(也可能是其他人)不理解问题。第0行有一个表格。第1行有一个进度条,并且表示“下载xxx进度”直到完成,然后切换为显示下一个项目的进度。您的表格中还有其他物品,但它们与前两项无关。当用户滚动表格以查看其他项目时,进度栏将被隐藏。即使在表中有一个单元格,并且您让用户滚动表格,您也希望进度条魔术般地保持可见状态。你能看到我的困惑吗? – 2012-08-10 12:52:45

+0

@DavidH:首先感谢您花时间做出回应。我的问题是当第一行开始下载时,progressview被显示出来。这很好。问题是当我在滚动uitableview的时候出现downaloding尚未完成行1号,现在当我再次看到第1行时,它不可见,即使它没有完成下载。我看不到在该行中的进度。在完成行号1的下降后,它将显示进度查看第2行是这个问题,请帮助 – user198725878 2012-08-10 13:30:50

回答

0

使用此方法

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

{ 
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d", indexPath.row]; 

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero]autorelease]; 

    UILabel *lblTitle, *lblAuthor; 





     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     lblTitle = [[[UILabel alloc]initWithFrame:CGRectMake(65, 10, 220, 25)]autorelease]; 
     lblTitle.backgroundColor = [UIColor clearColor]; 
     //lblTitle.font = [UIFont boldSystemFontOfSize:15.0]; 
     lblTitle.font = [UIFont fontWithName:@"Palatino-Bold" size:15.0]; 
     lblTitle.text = @"Sample title"; 
     [cell.contentView addSubview:lblTitle]; 

     lblAuthor = [[[UILabel alloc]initWithFrame:CGRectMake(65, 30, 220, 25)]autorelease]; 
     lblAuthor.backgroundColor = [UIColor clearColor]; 
     lblAuthor.font = [UIFont italicSystemFontOfSize:13.0]; 
     lblAuthor.textColor = [UIColor grayColor]; 
     lblAuthor.text = @"sample authior"; 
     [cell.contentView addSubview:lblAuthor]; 





    return cell; 
} 
+0

请解释我的代码 – user198725878 2012-08-10 13:40:47

+0

您使用的UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];当你向下滚动时,它在tableview中插入一个旧行。此方法将在每个索引处创建新行 – 2012-08-11 04:00:15

1

每次的的tableView问你要创建上面一个新的单元 - 它是不正确的,你shoulc回收细胞(也有例子苦苦在这里和其他地方在苹果文档如何做到这一点。)

现在,更新单元格中的进度栏​​有一些复杂性,你没有处理。首先,progressBar对象会随着表的滚动而改变 - 它会从你下面改变。比方说你有一个部分,使步骤更容易(但这些将与多个部分工作太):

所以,你应该只通过执行以下更新:

  • 您的下载要更新进度。它必须确定具有该进度条的表格行(如果将进度条添加到单元格,则在该视图上设置标签可以更容易地定位它)

  • 上面的代码然后要求UITableView可见单元格列表,并确定它需要更新的单元格是否可见 - 如果不是,则它不执行任何操作。它会要求单元格的progressBar(即[cell.contentView viewWithTag:...]),然后更新当前值(这个过程条可能以前会显示一些其他文件到期回收)。

在您的cellForRowAtIndexPath中:您试图回收一个单元格,如果失败,您将创建一个新单元格。在该代码之后,您不能对任何视图进行假设 - 您需要更新progressBar max,将当前值设置为当前下载值,更改任何标签文本等。

1

一对夫妇的意见:

  1. 你在ASINetworkQueue设置setDownloadProgressDelegate,但如果你想跟踪的个人下载的进度,你可能会想这样做的ASIHTTPRequest,来代替。这样你理论上可以得到多个下载相应的更新。

  2. 我说的原因之一是您目前正在创建networkQueue并添加一个ASIHTTPRequest并将其踢开。理论上你可以排队一大堆ASIHTTPRequest,然后将它们踢出去,每个更新自己的UIProgressView

  3. 最大的问题是您的cellForRowAtIndexPath

    • 首先,你不应该为每个使用唯一的单元格标识符。你失去了所有的内存优化;

    • 你应该检查,看看是否dequeueReusableCellWithIdentifier返回nil(如果没有您使用的原型细胞故事板的问题,但我假设你不这样做,在这里),如果没有,则创建表视图和你需要的任何控制;

    • 不管dequeueReusableCellWithIdentifier是否返回nil,您应该设置标签并更新进度条。和

    • 就个人而言,如果进度条已经存在,我发现只要删除进度条,然后再次添加,无论dequeueReusableCellWithIdentifier是否成功,都更容易。

  4. 你可能想保留“行”的对象,其中有,例如数组的轨道中,什么东西被下载,但更重要的是URL,一个保留UIProgressView对象(因为,烦人的downloadProgressDelegate是一个assign属性,不会为你保留,因此,你需要跟踪当前进行中的所有请求的进度条(不管是否可见,是否出列),而不是只是一个单一的UIProgressView

  5. 顺便说一下,它看起来像你正在调度的东西到后台队列,你并不真的需要这样做,因为ASINetworkQueue为你做所有这些东西。可能有一个技术原因,你可能会这样做,但我不认为这是必要的,因为我认为这些东西已经异步工作。如果你真的想在另一个队列中启动它,那很好,但只要记住将UI更新发送回主队列,因为你永远都不应该从后台进行UI更新。

仅供参考,下面的代码示例中,我使用,我经常使用tableviews,尤其是我的tableview有一个由NSArray中对象,其跟踪的什么,我需要的模型的代码模式为tableview的每个单元格。

@interface Row : NSObject 

@property (nonatomic, strong) NSString *author; 
@property (nonatomic, strong) NSString *title; 
@property (nonatomic, strong) UIProgressView *progress; 
@property (nonatomic, strong) NSString *urlString; 

@end 

我会使用它像如下:

@interface SampleViewController() 

@property (nonatomic, strong) NSArray *rows; 
@property (nonatomic, strong) ASINetworkQueue *networkQueue; 

@end 

@implementation SampleViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.rows = [self loadImageUrls]; 

    [self downloadStuff]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [self.rows count]; 
} 

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

#define kTitleTag 1 
#define kAuthorTag 2 
#define kProgressViewTag 3 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *CellIdentifier = @"asi"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    UILabel *lblTitle, *lblAuthor; 

    // if you could use storyboards, a lot of this silliness goes away 

    if (cell == nil) 
    { 
     // but in the world of nibs, you have to create the custom controls when you create the cell 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(65, 10, 220, 25)]; 
     lblTitle.backgroundColor = [UIColor clearColor]; 
     lblTitle.font = [UIFont boldSystemFontOfSize:15.0]; 
     //lblTitle.font = [UIFont fontWithName:@"Palatino-Bold" size:15.0]; 
     lblTitle.tag = kTitleTag; 
     [cell.contentView addSubview:lblTitle]; 

     lblAuthor = [[UILabel alloc]initWithFrame:CGRectMake(65, 30, 220, 25)]; 
     lblAuthor.backgroundColor = [UIColor clearColor]; 
     lblAuthor.font = [UIFont italicSystemFontOfSize:13.0]; 
     lblAuthor.textColor = [UIColor grayColor]; 
     lblAuthor.tag = kAuthorTag; 
     [cell.contentView addSubview:lblAuthor]; 
    } 
    else 
    { 
     // if you're recycling a cell, then link up with it's controls 

     cell.textLabel.backgroundColor = [UIColor clearColor]; 
     cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 

     lblTitle = (UILabel *)[cell.contentView viewWithTag:kTitleTag]; 
     lblAuthor = (UILabel *)[cell.contentView viewWithTag:kAuthorTag]; 

     UIProgressView *oldProgressView = (UIProgressView *)[cell.contentView viewWithTag:kProgressViewTag]; 
     if (oldProgressView && [oldProgressView isKindOfClass:[UIProgressView class]]) 
     { 
      [oldProgressView removeFromSuperview]; 
     } 
    } 

    Row *row = [self.rows objectAtIndex:indexPath.row]; 

    lblTitle.text = row.title; 
    lblAuthor.text = row.author; 

    if (row.progress) 
    { 
     row.progress.frame = CGRectMake(0, 55, 200, 25); 
     [cell.contentView addSubview:row.progress]; 
    } 

    return cell; 
} 

-(void)downloadStuff 
{  
    ASINetworkQueue *networkQueue = [[ASINetworkQueue alloc] init]; 

    [networkQueue setQueueDidFinishSelector:@selector(queueCompleted:)]; 
    [networkQueue setDelegate:self]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    [fileManager createDirectoryAtPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ASIHTTP"] 
      withIntermediateDirectories:NO 
          attributes:nil 
           error:nil]; 

    for (NSUInteger i = 0; i < [self.rows count]; i++) 
    { 
     Row *row = [self.rows objectAtIndex:i]; 

     row.progress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar]; 
     row.progress.tag = kProgressViewTag; 

     NSIndexPath *myIP = [NSIndexPath indexPathForRow:i inSection:0]; 
     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:myIP]; 
     if (cell) 
     { 
      // if the cell is visible, add the progress view to it 

      row.progress.frame = CGRectMake(0, 55, 200, 25); 
      [cell.contentView addSubview:row.progress]; 
     } 

     NSString *urlString = row.urlString; 

     ASIHTTPRequest *request; 
     request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlString]]; 
     NSString *filename = [urlString lastPathComponent]; 
     [request setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]]; 
     [request setDownloadDestinationPath:[[[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ASIHTTP"] stringByAppendingPathComponent:filename] stringByAppendingFormat:@"%1d", i]]; 
     [request setShouldContinueWhenAppEntersBackground:YES]; 
     [request setDelegate:self]; 
     [request setDidFinishSelector:@selector(requestDone:)]; 
     [request setDidFailSelector:@selector(requestWentWrong:)]; 
     [request setDownloadProgressDelegate:row.progress]; 
     [request setShowAccurateProgress:YES]; 

     [networkQueue addOperation:request]; 
    } 

    [networkQueue go]; 
} 

-(void)queueCompleted:(ASINetworkQueue*)queue 
{  
    NSLog(@"%s queueCompleted", __FUNCTION__); 
} 

- (void)requestDone:(ASIHTTPRequest *)request 
{ 
    NSLog(@"%s request done: %@", __FUNCTION__, [request responseString]); 

    [request.downloadProgressDelegate removeFromSuperview]; 

    // find which row it's on and remove it from that one 

    for (Row *row in self.rows) 
    { 
     if (row.progress == request.downloadProgressDelegate) 
      row.progress = nil; 
    } 

    // and let it go (obviously, non-ARC, release) 

    request.downloadProgressDelegate = nil; 
} 

- (void)requestWentWrong:(ASIHTTPRequest *)request 
{ 
    NSLog(@"%s request went wrong err = %@", __FUNCTION__, [request error]); 
} 

- (NSArray *)loadImageUrls 
{ 
    NSMutableArray *results = [[NSMutableArray alloc] init]; 

    for (NSInteger i = 0; i < 100; i++) 
    { 
     Row *row = [[Row alloc] init]; 
     row.urlString = @"http://host/test/bigfile"; 
     row.title = [NSString stringWithFormat:@"Title %1d", i]; 
     row.author = [NSString stringWithFormat:@"Author %1d", i]; 

     [results addObject:row]; 
    } 

    return results; 
} 

很显然,你必须自定义此为自己的目的(例如,我调整了自己的目的,人生苦短,我浪费时间编写非ARC代码;等等),但它向您展示了我个人如何亲自解决了我所看到的您所提供的代码示例的主要缺陷。我认为这需要相当长的延伸(如果我们离开,优雅地取消ASINetworkQueue,或许只为可见细胞加载ASIHTTPRequests等),但我会将其留给你。