2013-10-22 22 views
0

下午好。我在我的應用程序iCarousel中使用。目前我無法進行查看每個項目輪播。問題是,當我點擊「下載按鈕」時,Progress View先添加了一個項目。他看起來不錯並且有效,但是也出現在另一個視角的另一個項目中,它不應該在那裏。之後,當我再次單擊「下載按鈕」時,Progress View開始彼此混淆。請告訴我我做錯了什麼,以及我如何行事?我是新手Objective-C。在iCarousel中添加獨立的Progress VIew以指示下載

P.S要下載我使用AFNetworking的數據。

iCarousel:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view 
{ 
    NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"]; 
    NSDictionary *myDic =[magazinesInfo objectAtIndex:index]; 

    //Change image size 
    UIImage *img = [UIImage imageWithImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@_img.png",docDir,[myDic objectForKey:@"title"]]] scaledToSize:CGSizeMake(370,513)]; 

    UIImageView *faceImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,768,1004)]; 
    UIImage *dwImage = [UIImage imageNamed:@"button.png"]; 
    UIImage *readImage = [UIImage imageNamed:@"read_button.png"]; 
    UIImage *deleteImage = [UIImage imageNamed:@"delete_button.png"]; 
    UIImage *cancelImage = [UIImage imageNamed:@"cancelButton.png"]; 

    if(view ==nil) 
    { 
     UILabel *nomer = [[UILabel alloc] initWithFrame:CGRectMake(345, 85+MY_OFFSET, 75, 29)]; 
     UILabel *nameMag = [[UILabel alloc] initWithFrame:CGRectMake(55, 720+MY_OFFSET, 658, 80)]; 
     UILabel *dateMag = [[UILabel alloc] initWithFrame:CGRectMake(55, 821+MY_OFFSET, 658, 23)]; 

     UIButton *downloadButton = [[UIButton alloc] initWithFrame:CGRectMake(321, 890+MY_OFFSET, 128, 37)]; 
     UIButton *readButton = [[UIButton alloc] initWithFrame:CGRectMake(246, 890+MY_OFFSET, 128, 37)]; 
     UIButton *deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(385, 890+MY_OFFSET, 128, 37)]; 

     UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0+MY_OFFSET, 128, 37)]; 


     view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 1004)]; 
     view = faceImage; 
     faceImage.image = nil; 

     ((UIImageView *)view).image = nil; 
     view.contentMode = UIViewContentModeCenter; 


     //Magazine number 

     nomer.backgroundColor = [UIColor clearColor]; 
     nomer.textAlignment = NSTextAlignmentCenter; 
     [nomer setFont:[UIFont fontWithName:@"OpenSans-Light" size:36.0f]]; 
     nomer.textColor = [UIColor whiteColor]; 
     nomer.tag = 1; 

     //Magazine name 

     nameMag.backgroundColor = [UIColor clearColor]; 
     nameMag.textAlignment = NSTextAlignmentCenter; 
     [nameMag setFont:[UIFont fontWithName:@"OpenSans-Light" size:30.0f]]; 
     nameMag.numberOfLines=2 ; 
     nameMag.textColor = [UIColor blackColor]; 
     nameMag.tag = 3; 

     //Date magazine 

     dateMag.backgroundColor = [UIColor clearColor]; 
     dateMag.textAlignment = NSTextAlignmentCenter; 
     [dateMag setFont:[UIFont fontWithName:@"OpenSans-Light" size:20.0f]]; 
     dateMag.textColor = [UIColor blackColor]; 
     dateMag.tag = 4; 


     //Download button 

     [downloadButton setBackgroundImage:dwImage forState:UIControlStateNormal]; 
     [downloadButton addTarget:self action:@selector(pressDownload:) forControlEvents:UIControlEventTouchUpInside]; 
     downloadButton.tag = 5; 
     downloadButton.hidden = YES; 

     //Read button 

     [readButton setBackgroundImage:readImage forState:UIControlStateNormal]; 
     [readButton addTarget:self action:@selector(readMag:) forControlEvents:UIControlEventTouchUpInside]; 
     readButton.hidden=YES; 
     readButton.tag = 8; 

     //Delete button 

     [deleteButton setBackgroundImage:deleteImage forState:UIControlStateNormal]; 
     [deleteButton addTarget:self action:@selector(deleteMag:) forControlEvents:UIControlEventTouchUpInside]; 
     deleteButton.hidden=YES; 
     deleteButton.tag = 9; 

     [cancelButton setBackgroundImage:cancelImage forState:UIControlStateNormal]; 
     [cancelButton addTarget:self action:@selector(deleteMag:) forControlEvents:UIControlEventTouchUpInside]; 
     cancelButton.hidden=NO; 
     cancelButton.tag = 10; 


     //Add label to view 
     [view addSubview:nomer]; 
     [view addSubview:nameMag]; 
     [view addSubview:dateMag]; 


     //Add button to view 
     [view addSubview:downloadButton]; 
     [view addSubview:readButton]; 
     [view addSubview:deleteButton]; 
     [view addSubview:cancelButton]; 

    } 

    else 
    { 
     //Set tag to image 
     ((UIImageView *)faceImage).image = (UIImage*)[view viewWithTag:2]; 

     //Set tag to label 
     [[[view subviews]objectAtIndex:0]viewWithTag:1]; 
     [[[view subviews]objectAtIndex:1]viewWithTag:3]; 
     [[[view subviews]objectAtIndex:2]viewWithTag:4]; 


     //Set tag to button 

     [[[view subviews]objectAtIndex:3]viewWithTag:5]; 
     [[[view subviews]objectAtIndex:4]viewWithTag:8]; 
     [[[view subviews]objectAtIndex:5]viewWithTag:9]; 
     [[[view subviews]objectAtIndex:6]viewWithTag:10]; 

    } 

    //Hide button download and show read,delete button 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if ([fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/%@_mag.pdf",docDir,[myDic objectForKey:@"title"]]] == YES) 
    { 

     [[[view subviews] objectAtIndex:4] setHidden:NO]; 
     [[[view subviews] objectAtIndex:5] setHidden:NO]; 
     [[[view subviews] objectAtIndex:3] setHidden:YES]; 

    } 
    else 
    { 
     [[[view subviews] objectAtIndex:4] setHidden:YES]; 
     [[[view subviews] objectAtIndex:5] setHidden:YES]; 
     [[[view subviews] objectAtIndex:3] setHidden:NO]; 
    } 

    //Hide date and name of magazine when view changed 
    if (index != [self.carousel currentItemIndex]) { 
     [[[view subviews]objectAtIndex:1]setHidden:YES]; 
     [[[view subviews]objectAtIndex:2]setHidden:YES]; 
    } 
    else{ 
     [[[view subviews]objectAtIndex:1]setHidden:NO]; 
     [[[view subviews]objectAtIndex:2]setHidden:NO]; 
    } 


    ((UIImageView *)view).image = img; 

    UILabel *nomer = [[view subviews]objectAtIndex:0]; 
    nomer.text = [myDic objectForKey:@"title"]; 

    UILabel *nameMag = [[view subviews]objectAtIndex:1]; 
    nameMag.text = @"Жить интересно!」 №5 Путешествия как стиль жизни"; 

    UILabel *dateMag = [[view subviews]objectAtIndex:2]; 
    dateMag.text = [myDic objectForKey:@"date"]; 


    return view; 
} 

下載按鈕動作:

- (IBAction)pressDownload:(id)sender 
{ 


    NSLog(@"download button was pressed"); 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSDictionary *myDic = [magazinesInfo objectAtIndex:curID]; 
    NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"]; 
    NSString *pdfFilePath = [NSString stringWithFormat:@"%@/%@_mag.pdf.tmp",docDir,[myDic objectForKey:@"title"]]; 
    NSString *newPdfNamePath = [NSString stringWithFormat:@"%@/%@_mag.pdf",docDir,[myDic objectForKey:@"title"]]; 

    //Test for Progress bar 
    UIButton *pressedButton = (UIButton *)sender; 

    UIView *superViewOfPressedButton = pressedButton.superview; 

    UIProgressView *downloadProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(300, 950, 127, 8)]; 
    UILabel *downloadPrecent = [[UILabel alloc]initWithFrame:CGRectMake(430, 950, 60, 20)]; 


    [superViewOfPressedButton addSubview:downloadProgress]; 
    [superViewOfPressedButton addSubview:downloadPrecent]; 


    [downloadProgress setHidden:NO]; 
    [downloadPrecent setHidden:NO]; 


    NSLog(@"%@",sender); 


    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[myDic objectForKey:@"magazine"]]]; 
    AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:pdfFilePath append:NO]; 

    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) 
    { 
      downloadProgress.progress = (float)totalBytesRead/totalBytesExpectedToRead; 
      downloadPrecent.text =[NSString stringWithFormat:@"%1.0f%@ ",((float)totalBytesRead/totalBytesExpectedToRead)*100,@"%"]; 
    }]; 

    [operation setCompletionBlock:^{ 

     [fileManager moveItemAtPath:pdfFilePath toPath:newPdfNamePath error:NULL]; 
     [fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@_mag.pdf.tmp",docDir,[myDic objectForKey:@"title"]] error:NULL]; 

     [downloadProgress setHidden:YES]; 
     [downloadPrecent setHidden:YES]; 

     NSLog(@"downloadComplete!"); 
     [carousel reloadData]; 

    }]; 
    [operation start]; 

} 

要北斗星: 很抱歉,但我不明白你的問題。我在下載數據的方法中從字典中傳遞URL。在方法iCarousel我給你發展觀標籤= 7,然後我添加一個條件來隱藏像這樣:

if (([fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/%@_mag.pdf.tmp",docDir,[myDic objectForKey:@"title"]]] == YES) && (index == [self.carousel currentItemIndex])) 
     { 
      [[[view viewWithTag:7] setHidden:NO]; 
      [[[view viewWithTag:7] setHidden:NO]; 

     } 
     else 
     { 
      [[[view viewWithTag:7] setHidden:YES]; 
      [[[view viewWithTag:7] setHidden:YES]; 

     } 

回答

0

不要創建和pressDownload:添加進度視圖。相反,在創建視圖時創建它,但將其設置爲hidden。然後,當你需要它時,只需取消隱藏它。當您重新使用視圖時,請務必根據您是否在該索引處下載項目來設置適當的值。

+0

我試過這樣做。我在viewForItemAtIndex中創建了一個進度視圖,以及按鈕和一個標籤,給它們標籤。但它不會導致不好,錯誤是一樣的。如果你點擊第一個下載視圖,進度視圖會出現在它上面,甚至在第三個視圖上,爲什麼我不明白。如果您可以編寫必須顯示或隱藏的條件,則不需要進度視圖。 – Emmett

+0

重複使用視圖時,您需要隱藏進度視圖,這是不恰當的 - 直到你這樣做,你會看到相同的問題。這是一個視圖重用問題... – Wain

+0

你的數據模型應該告訴你,你正在下載一個指定索引的項目,是嗎? – Wain