坚持进步状态吧

时间:2013-01-03 01:00:25

标签: objective-c asihttprequest uiprogressview

我正在使用ASIHttpRequest在我的应用程序中下载文档。我正在显示下载的每个文档的progessbar。文档显示在UITableView中,因此表格中的每个文档单元都有一个单独的进度条。当用户点击时在“返回”然后再次进入文档表,我想显示进度条。但是没有显示。下载文档的代码如下:

- (void)startDownloadingDocumentUsingIndexPath:(NSIndexPath*)indexPath 
{   
    Document *document = [self.objects objectAtIndex:indexPath.row];
    self.ip=indexPath;
    if(isNetworkAvailable)
    {   
        NSString *str = [NSString stringWithFormat:kBody_convertDocToByte,
                         document.docCatId,
                         document.docCatName,
                         document.docChksum,
                         document.docLocation,
                         document.docName,
                         document.docSize,
                         document.docType,
                         document.docUpdated,
                         document.docUpdatedBy,
                         document.documentId
                         ];
        NSMutableData *data = [NSMutableData dataWithData:[str dataUsingEncoding:NSUTF8StringEncoding]];
        self.req = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:kURL]];

        self.req.shouldAttemptPersistentConnection   = NO;
        [self.req setDownloadProgressDelegate:_progress];

        self.req.delegate=APP_DEL;
        self.req.timeOutSeconds=timeOut;
        [self.req setDidFinishSelector:@selector(didFinishDownloadDocument:)];
        [self.req setDidFailSelector:@selector(didFailDownloadDocument:)];
        [self.req addRequestHeader:@"SOAPAction" value:kAction_convertDocToByte];
        [self.req addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
        [self.req setPostBody:data];
        [self.req setRequestMethod:@"POST"];
        [self.req setContentLength:[data length]];

        self.req.userInfo=[NSDictionary dictionaryWithObjectsAndKeys:document,@"data",self.objCategory.categoryName,@"catname",self,@"vctr", nil];
        [[APP_DEL nq] addOperation:self.req];
        [[APP_DEL nq] go];
        [APP_DEL nq].showAccurateProgress = YES;
    } 
    else {
        UIAlertView *av=[[UIAlertView alloc] initWithTitle:@"Message" message:@"Please connect to internet & try downloading/updating document later." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [av show];
    }
}

我应该在代码中进行哪些更改,以便持久保存进度条的状态?

编辑: - 文档表实际上是一个“子”表。当用户在“父”表中选择一行时显示。“父”表中的代码为:< / p>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.nxt_obj_STMaster2ViewController=[[STMaster2ViewController alloc] initWithNibName:@"STMaster2ViewController" bundle:nil];
    self.nxt_obj_STMaster2ViewController.objCategory=[self.objects objectAtIndex:indexPath.row];
    self.nxt_obj_STMaster2ViewController.managedObjectContext=self.managedObjectContext;
    self.nxt_obj_STMaster2ViewController.detailViewController=self.detailViewController;
    self.nxt_obj_STMaster2ViewController.pre_STMasterViewController=self;

    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    [self.navigationController pushViewController:self.nxt_obj_STMaster2ViewController animated:YES];
}

'child'文档视图控制器的viewDidLoad如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(button_refreshTapped:)];

    self.navigationItem.rightBarButtonItem=item;

    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"left-background.jpg"]];
    [tempImageView setFrame:self.tableView.frame];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    self.tableView.backgroundView = tempImageView;

    [self startLoadingData:self.detailViewController.liveData];
}

1 个答案:

答案 0 :(得分:1)

所以,你每次拨打tableView:didSelectRowAtIndexPath:时都在创建一个全新的闪亮视图控制器。相反,您应该只创建一次并在需要时重新显示它。我不确定你的所有逻辑,所以你可能需要在更改行时创建一个新的(在这种情况下,你应该比较你的.objCategory以查看它是否与你的视图控制器相同你即将推动。)

尝试将其更改为:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.nxt_obj_STMaster2ViewController)
    {
        self.nxt_obj_STMaster2ViewController=[[STMaster2ViewController alloc] initWithNibName:@"STMaster2ViewController" bundle:nil];
        self.nxt_obj_STMaster2ViewController.managedObjectContext=self.managedObjectContext;
        self.nxt_obj_STMaster2ViewController.detailViewController=self.detailViewController;
        self.nxt_obj_STMaster2ViewController.pre_STMasterViewController=self;
    }

    self.nxt_obj_STMaster2ViewController.objCategory=[self.objects objectAtIndex:indexPath.row];

    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    [self.navigationController pushViewController:self.nxt_obj_STMaster2ViewController animated:YES];
}