6

我认为这是一个相当复杂的问题。我有一个显示许多可下载内容的 TableView。当您单击单元格内的按钮时,下载开始。

但是我遇到了几个问题:1.如何确保始终显示进度条(即使用户滚动滚动并且重新加载单元格) 2.如何确保用户可以下载2个文件一次。我担心它会导致问题,因为我使用了一些实例变量。在某种程度上,它应该有点像在音乐应用程序中从 iCloud 下载

这是我的代码

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];   
    }
    //cell.tag = indexPath.row*10;
    Uebungsblaetter *uebungCell = [uebungsblattArray objectAtIndex:indexPath.row];
    cell.tag = indexPath.row*10;
    cell.textLabel.text = [self getFileNameOutOf:uebungCell.url];
    cell.textLabel.textColor = [UIColor grayColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIButton *dl = [UIButton buttonWithType:UIButtonTypeCustom];
    dl.tag = indexPath.row*10;
    [dl setBackgroundImage:[UIImage imageNamed:@"downloadButton.png"] forState:UIControlStateNormal];
    [dl setBackgroundImage:[UIImage imageNamed:@"downloadButtonH.png"] forState:UIControlStateHighlighted];
    [dl setFrame:CGRectMake(230.0, (cell.frame.size.height-28)/2, 28, 28)];
    [dl addTarget:self action:@selector(downloadFileWhenPressedButton:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:dl];
    UIProgressView *dlProgress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    dlProgress.frame = CGRectMake(cell.frame.size.width-150, 17, 50, 9);
    dlProgress.tag =indexPath.row*10+1;
    dlProgress.progress = 0.0;
    [cell.contentView addSubview:dlProgress];
    [dlProgress setHidden:YES];   

    return cell;
}

//download methods
- (void)downloadFileWhenPressedButton:(UIButton*)sender{
    sender.hidden = YES;
    dlIndex = sender.tag/10;
    Uebungsblaetter *selectedUB = [uebungsblattArray objectAtIndex:dlIndex];
    NSURL *theUrl = [NSURL URLWithString:selectedUB.url];
    NSURLRequest *req=[NSURLRequest requestWithURL:theUrl cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:120];
    dlCell = (UITableViewCell *)[[sender superview]superview];
    currDlProgress = (UIProgressView* )[dlCell.contentView viewWithTag:dlIndex*10+1];
    currDlProgress.hidden = NO;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    dlFilePath = [NSString stringWithFormat:@"%@/%@_%@", [paths objectAtIndex:0],self.courseLable.text,[self getFileNameOutOf:selectedUB.url]];
    NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
    if (con) {
        myWebData = [NSMutableData data];
    }

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = YES;
    currDlProgress.progress = 0;
    _totalFileSize = response.expectedContentLength;
    NSLog(@"%@",@"connection established");
    [myWebData setLength: 0];



}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    _receivedDataBytes += [data length];
    currDlProgress.progress = _receivedDataBytes / (float)_totalFileSize;

    NSLog(@"%@",@"connection receiving data");
    [myWebData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"%@",@"connection failed");
    //  [AlertViewHandler showAlertWithErrorMessage:@"Sorry, there is no network connection. Please check your network and try again."];
    //  [self parserDidEndDocument:nil];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = NO;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.35];
    [currDlProgress setAlpha:0];
    [UIView commitAnimations];
    [myWebData writeToFile:dlFilePath atomically:YES];
    Uebungsblaetter *loadedUB = [uebungsblattArray objectAtIndex:dlIndex];
    loadedUB.downloaded = [NSNumber numberWithBool:YES];
    [courseTable reloadData];
}

如果有人有线索或很好的代码示例会很好

4

2 回答 2

22

重要的是要意识到您的进度条不会一直显示(即用户可以滚动表格,一旦离开屏幕,同一单元格就可以在另一个索引位置重复使用以获取不同的内容)。因此,您需要做的是在某个地方存储有关任何活动下载的数据,包括表中的索引位置、总文件大小以及到目前为止下载的字节数。然后,无论何时绘制单元格,您都需要检查该单元格的项目当前是否正在下载,如果是,则显示具有适当进度百分比的栏。

最简单的方法是向视图控制器添加一个属性来存储此信息。它可以是一个包含对象NSMutablerray集合的NSMutableDictionary对象,每个字典都将包含有关活动下载的必要信息。

@property (nonatomic, strong) NSMutableArray *activeConnections;

首先,您将在以下位置初始化数组viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //...

    self.activeConnections = [[NSMutableArray alloc] init];
}

每当按下按钮时,您都会将一个 NSMutableDictionary 对象添加到您的数组中,其中包含您需要的信息。

- (void)downloadFileWhenPressedButton:(UIButton*)sender
{
    // ...

    //  then create dictionary
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setObject:con forKey:@"connection"];  // save connection so we can reference later
    [dict setObject:[NSNumber numberWithInt:[sender.tag]/10] forKey:@"row"];  // this is the row index from your table
    [dict setObject:[NSNumber numberWithInt:999] forKey:@"totalFileSize"];  // dummy size, we will update when we know more
    [dict setObject:[NSNumber numberWithInt:0] forKey:@"receivedBytes"];

    [self.activeConnections addObject:dict];
}

此外,我们将创建两个实用方法,以便我们可以使用连接对象本身或表中的行索引位置轻松地从数组中检索连接信息。

- (NSDictionary*)getConnectionInfo:(NSURLConnection*)connection
{
    for (NSDictionary *dict in self.activeConnections) {
        if ([dict objectForKey:@"connection"] == connection) {
            return dict;
        }
    }
    return nil;
}

- (NSDictionary*)getConnectionInfoForRow:(int)row
{
    for (NSDictionary *dict in self.activeConnections) {
        if ([[dict objectForKey:@"row"] intValue] == row) {
            return dict;
        }
    }
    return nil;
}

建立连接后,使用预期长度更新您的字典 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // ...

    NSDictionary *dict = [self getConnectionInfo:connection];
    [dict setObject:[NSNumber numberWithInt:response.expectedContentLength] forKey:@"totalFileSize"];
}

当您接收数据时,您将更新接收到的字节数并告诉您的 tableView 重绘包含进度条的单元格。

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // ...

    NSDictionary *dict = [self getConnectionInfo:connection];
    NSNumber bytes = [data length] + [[dict objectForKey:@"receivedBytes"] intValue];

    [dict setObject:[NSNumber numberWithInt:response.expectedContentLength] forKey:@"receivedBytes"];

    int row = [[dict objectForKey:@"row"] intValue];
    NSIndexPath *indexPath = [NSIndexPathindexPathForRow:row inSection:0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                          withRowAnimation:UITableViewRowAnimationNone];
}

当你的连接下载完成后,你应该从你的 activeConnections 数组中删除连接,然后重新加载表格单元格。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    // ...

    NSDictionary *dict = [self getConnectionInfo:connection];
    [self.activeConnections removeObject:dict];

    int row = [[dict objectForKey:@"row"] intValue];
    NSIndexPath *indexPath = [NSIndexPathindexPathForRow:row inSection:0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                          withRowAnimation:UITableViewRowAnimationNone];
}

最后,cellForRowAtIndexPath:您需要根据 activeConnections 数组中的信息绘制单元格的进度条。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ...

    // remove any previous buttons or progress bars from this cell
    for (UIView *view in [cell.contentView subViews]) {

        if ([view isKindOfClass:[UIProgressView class]] || [view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperView];
        }
    }

    // look for active connecton for this cell
    NSDictionary *dict = [self getConnectionInfoForRow:indexPath.row];

    if (dict) {
        // there is an active download for this cell, show a progress bar

        UIProgressView *dlProgress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        dlProgress.frame = CGRectMake(cell.frame.size.width-150, 17, 50, 9);
        dlProgress.tag = indexPath.row*10+1;
        dlProgress.progress = [[dict objectForKey:@"receivedBytes"] intValue] / [[dict objectForKey:@"totalFileSize"] intValue];

        [cell.contentView addSubview:dlProgress];

    } else {
        // no active download, show the download button

        UIButton *dl = [UIButton buttonWithType:UIButtonTypeCustom];
        dl.tag = indexPath.row*10;
        [dl setBackgroundImage:[UIImage imageNamed:@"downloadButton.png"] forState:UIControlStateNormal];
        [dl setBackgroundImage:[UIImage imageNamed:@"downloadButtonH.png"] forState:UIControlStateHighlighted];
        [dl setFrame:CGRectMake(230.0, (cell.frame.size.height-28)/2, 28, 28)];
        [dl addTarget:self action:@selector(downloadFileWhenPressedButton:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:dl];
    }
}
于 2012-08-31T05:17:42.603 回答
3

您的问题是您混淆了数据和 UI。您依靠每个表格视图单元格来存储它的当前下载状态,这会导致问题。相反,您应该创建一个自定义对象,该对象可以存储单元格所需的数据(即表示是否正在下载、当前下载进度、下载数据等的布尔值)。然后,您应该拥有这些对象的可变数组,例如objectAtIndex:0表中第一个单元格的数据,依此类推。

当下载开始时,更新数组中的相关对象。理想情况下,当您创建表格视图单元格时,您应该使用该NSKeyValueObserving协议,以便在其基础数据发生更改时自动通知它们并且可以适当地更新其数据。当一个单元格被重用时会很好,因为存储它的数据的对象仍然存在,如果它需要再次制作,因为它即将回到屏幕上,那么它将能够使用该数据来正确显示。

如果您需要对此任何部分的任何进一步解释,请告诉我。

于 2012-08-31T00:47:46.627 回答