0

Good afternoon. I use in my application iCarousel . At the moment I can not make Progress View each item carousel. The problem is that when I click on the "Download button" Progress View have added an item first . He appears fine and works , but also appears in another 2 item from another view, where it should not be . After that, when I again click the " download button " Progress View begin to be confused with each other. Please tell me exactly what I 'm doing wrong and how I act of intercourse ? I'm new to objective-c.

P.S To download the data I use 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;
}

Download button action:

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

}

To Wain: Sorry but I can not understand your question. I pass the URL from the dictionary in the method of downloading data. In Method iCarousel I assign Progress View tag = 7 then I add a condition to hide like this:

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

        }
Emmett
  • 464
  • 5
  • 11

1 Answers1

0

Don't create and add the progress view in pressDownload:. Instead, create it when the view is created but set it to hidden. Then when you need it just un-hide it. When you reuse a view be sure to set the appropriate value based on whether you are downloading the item at that index.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I have tried to do so. I have created a Progress View in viewForItemAtIndex, as well as buttons and one label, giving them tags. But it is not good it will not result, the error was the same. If you click on the first download View, Progress View will appear on it, and even on the third, why is it I do not understand. If you can write the condition in which I have to show or hide I do not want Progress View. – Emmett Oct 22 '13 at 18:36
  • You need to hide the progress view when reusing views and it isn't appropriate - until you do that you will see the same issue. It is a view reuse problem... – Wain Oct 22 '13 at 18:38
  • You data model should tell you that you are downloading an item for a specified index, yes? – Wain Oct 22 '13 at 18:38
  • What's the purpose of `&& (index == [self.carousel currentItemIndex])` ? Probably want to remove that... – Wain Oct 22 '13 at 19:10