Check progress where is necessary UITableView
我在保存
解释:
服务器上有一个 API,我从中获取数据,然后将其存储在
这是我的代码。
cellForRowAtIndexPath 方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"readyCell"; AVMMovieCell *cell = [self.readyTable dequeueReusableCellWithIdentifier:CellIdentifier]; // Configure the cell... if (cell == nil) { cell = (AVMMovieCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; } AVMFilmsStore *oneItem; oneItem = [readyArray objectAtIndex:indexPath.row]; NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)indexPath.row]; if (oneItem.ready==1){ cell.progressLabel.hidden = YES; cell.progressLine.hidden = YES; if ([selecedCellsArray containsObject:[NSString stringWithFormat:@"%@",rowNsNum]] ) { if (![cell.progressLabel.text isEqualToString:@""]&& ![cell.progressLabel.text isEqualToString:@"Success"] && ![cell.progressLabel.text isEqualToString:@"Creating"]){ cell.progressLabel.hidden = NO; cell.progressLine.hidden = NO; } else { cell.progressLabel.hidden = YES; cell.progressLine.hidden = YES; } } else{ if(!oneItem.isProcessing && !cell.selected){ cell.progressLabel.hidden = YES; cell.progressLine.hidden = YES; } } } else { //if processing if (![processingCellsArray containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]){ [processingCellsArray addObject:[NSString stringWithFormat:@"%@",rowNsNum]]; if (!cell.isSelected){ [cell setSelected:YES]; } cell.progressLabel.hidden = NO; cell.progressLine.hidden = NO; NSArray * arrayOfThingsIWantToPassAlong = [NSArray arrayWithObjects: cell, oneItem, indexPath, nil]; if(!isMaking){ [self performSelector:@selector(getProgress:) withObject:arrayOfThingsIWantToPassAlong afterDelay:0]; } else{ [self performSelector:@selector(getProgress:) withObject:arrayOfThingsIWantToPassAlong afterDelay:0.5]; } isMaking = YES; } else { if (!cell.isSelected){ [cell setSelected:YES]; } cell.progressLabel.hidden = NO; cell.progressLine.hidden = NO; NSArray * arrayOfThingsIWantToPassAlong = [NSArray arrayWithObjects: cell, oneItem, indexPath, nil]; if(!isMaking){ [self performSelector:@selector(getProgress:) withObject:arrayOfThingsIWantToPassAlong afterDelay:0]; } else{ [self performSelector:@selector(getProgress:) withObject:arrayOfThingsIWantToPassAlong afterDelay:0.3]; } isMaking = YES; } } return cell; } |
和 getProgress 方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | -(void)getProgress:(NSArray*)args{ if (progManager == nil && !progStop){ __block AVMFilmsStore * oneItem = args[1]; if(!oneItem.isLocal){ __block AVMMovieCell * cell = args[0]; __block NSIndexPath *indexPath = args[2]; progManager = [AFHTTPRequestOperationManager manager]; __block NSString *token = [defaults objectForKey:@"token"]; __block NSString *header = [NSString stringWithFormat:@"Bearer %@",token]; __block NSDictionary *params = @{@"lang": NSLocalizedString(@"lang",nil),@"project":oneItem.fileId}; __block NSString *oneHundredPercent; __block NSString *progIsText; progManager.responseSerializer = [AFJSONResponseSerializer serializer]; [progManager.requestSerializer setValue:header forHTTPHeaderField:@"Authorization"]; if(cell.selected || isMaking) { //if I just check for"cell.selected" is always"NO" [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; [progManager POST:@"http://example.com/api/project/get-progress" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { if ([[responseObject objectForKey:@"result"]isEqualToString:@"success"]){ progCreate = [responseObject objectForKey:@"progress"]; oneHundredPercent = @"100"; if ([progCreate intValue]==[oneHundredPercent intValue]){ if([processingCellsArray containsObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]]){ [processingCellsArray removeObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]]; [cell setSelected:NO]; } [readyArray removeAllObjects]; [defaults setObject:@"false" forKey:@"isSomethingInSort"]; isMaking = NO; [self getReadyMovies:progIsText nameLabel:oneItem.fileName]; } else{ if([progCreate intValue]>=50){ if([progCreate intValue]>=60){ self.navigationController.navigationItem.leftBarButtonItem.enabled = YES; createMainButton.enabled = YES; } [[NSNotificationCenter defaultCenter] postNotificationName:@"gotFiftyNote" object:@"50"]; [cell.progressLine setProgress:[progCreate floatValue]/100 animated:YES]; } else { [cell.progressLine setProgress:progUploadLimit]; } progManager = nil; progManager.responseSerializer = nil; progManager.requestSerializer = nil; token = nil; header = nil; params = nil; progIsText = nil; oneItem = nil; cell = nil; indexPath = nil; isMaking = YES; progCreate = nil; oneHundredPercent = nil; [self getProgress:args]; } } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; NSLog(@"Error: %@", error.localizedDescription); }]; } } } } |
任何建议都会对我有所帮助。我为这个问题头疼了两周。
我看到了您的代码,但使用这些大型方法有点难以理解。我不会跟踪数组中的处理单元。每个单元格都有一个对象来表示,这些对象有一个准备好的布尔值和一个进度值,对吧?所以尝试这样的事情:
- 确保每个单元格都有一个 progressView 作为子视图。
- 您的单元类应该有一个名为 styleForReady:(bool)isReady andProgress:(nsinteger)progress 的公共方法
- 为每个模型拨打服务电话以查看它们是否已完成。每当该服务调用返回时,您只需更新数组中的模型对象,并在它们具有新的进度值后执行 [self.tableView reloadData]。这将触发 numberOfRows(它应该返回 arrayOfObjects.count)和 cellForRowAtIndexPath:(它应该为那个 indexPath 出列单元格,获取代表那个单元格的模型,比如 arrayOfObjects[indexPath.row],最后,调用单元格来设置自己的样式基于该模型做 [cell styleForReady:objectModel.ready andProgress:objectModel.progress])
请记住,控制器应该只跟踪模型对象,将单元格出列并告诉单元格设置样式传递模型。不要将所有逻辑都放在控制器中。