, (.. , , , ). - , , . , , , , , , .
- . NSMutablerray, NSMutableDictionary, .
@property (nonatomic, strong) NSMutableArray *activeConnections;
viewDidLoad::
- (void)viewDidLoad
{
[super viewDidLoad];
self.activeConnections = [[NSMutableArray alloc] init];
}
, , NSMutableDictionary , .
- (void)downloadFileWhenPressedButton:(UIButton*)sender
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:con forKey:@"connection"];
[dict setObject:[NSNumber numberWithInt:[sender.tag]/10] forKey:@"row"];
[dict setObject:[NSNumber numberWithInt:999] forKey:@"totalFileSize"];
[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
{
for (UIView *view in [cell.contentView subViews]) {
if ([view isKindOfClass:[UIProgressView class]] || [view isKindOfClass:[UIButton class]]) {
[view removeFromSuperView];
}
}
NSDictionary *dict = [self getConnectionInfoForRow:indexPath.row];
if (dict) {
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 {
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];
}
}