导航到另一个viewcontroller时,Label会停止更新

时间:2016-08-19 10:59:23

标签: ios uitableview

我在服务器上上传文件并在标签上显示percention,标签在表格单元格中,但是当我们导航到另一个视图时,它的停止更新,更新百分比仅在我们滚动表格时显示..

enter image description here

-(void)sendReplyOnServer:(NSMutableDictionary*)params
         withMessage:(Message*)sendMessage
           withIndex:(NSUInteger)deliveredIndex {


[self addReplyInDiscussion:sendMessage.disscussionId];

[[APIClient sharedClient] postReply:params andBlock:^(NSDictionary *responseDictionary,
                                                      NSError *error) {
    @try {

        [[DataManager sharedManager].sendMessages removeObject:sendMessage];

        if (!error){


            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary :[[responseDictionary objectForKey:@"response"] objectForKey:@"data"] ];
            //  [dict setObject:@"0" forKey:@"message_status"];

            NSLog(@"%@",dict);

            Message *deliveredMessage =
            [Message modelObjectWithDictionary:dict];

            if ([[responseDictionary objectForKey:@"response"] objectForKey:@"document"]){

                Document *doc = [Document modelObjectWithDictionary:[[responseDictionary objectForKey:@"response"] objectForKey:@"document"]];

                if ([DataManager sharedManager].documentListArray.count == 0){
                    [DataManager loadDocumentList];
                }
                if (![DataManager sharedManager].allDocumentsList){
                    [DataManager sharedManager].allDocumentsList = [[NSMutableArray alloc] init];
                }

                NSString *documentID = @"1";
                NSPredicate *predicate = [NSPredicate predicateWithFormat:@"documentId == %@",documentID];
                NSArray *filteredArray = [[DataManager sharedManager].documentListArray filteredArrayUsingPredicate:predicate];
                if (filteredArray.count != 0) {
                    Document *cameraRoll = [filteredArray objectAtIndex:0];
                    doc.parent = cameraRoll;
                    [cameraRoll.documents addObject:doc];
                }
//                    Document *cameraRoll = [[DataManager sharedManager].documentListArray objectAtIndex:1];
//                    [cameraRoll.documents addObject:doc];

                [[DataManager sharedManager].allDocumentsList addObject:doc];
                [DataManager serializeDocumentList];
                [DataManager serializeallDocumentList];

            }

            //                                                      if ([ deliveredMessage.deliveryStatus isEqualToString:@"1" ]) {
            deliveredMessage.deliveryStatus = @"2";

            //image save in file
           // deliveredMessage.attachment.image = sendMessage.attachment.image;

            if(sendMessage.attachment.image) {


                deliveredMessage.attachment.image = sendMessage.attachment.image;

                NSURL *url = [NSURL URLWithString:deliveredMessage.attachment.fileName];
                NSString *name = [url lastPathComponent];

                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:name];
                // NSLog(@"my path == %@",path);
                NSData * binaryImageData = UIImagePNGRepresentation(sendMessage.attachment.image);

                [binaryImageData writeToFile:path atomically:YES];
            }

            // }

            if ([sendMessage.disscussionId isEqualToString:disscussionId]) {


                [self updateMessageList:deliveredMessage withIndex:deliveredIndex withSendMessage:sendMessage];

            } else if ([disscussionId isEqualToString:@""] ) {
                NSLog(@"reached in known");

                NSString *string  = [NSString stringWithFormat:@"dataIdentifier == %@ ",sendMessage.disscussionId];

                NSPredicate *predicate = [NSPredicate predicateWithFormat:string];

                NSArray *filterArray = [[DataManager sharedManager].chatListArray filteredArrayUsingPredicate:predicate];

                if (filterArray.count != 0 ) {

                    DiscussionData *discussionOject = [filterArray objectAtIndex:0];
                    NSUInteger indexOfManager = [[DataManager sharedManager].chatListArray indexOfObject:discussionOject];
                    DiscussionData *managerDiscussionOject = [[DataManager sharedManager].chatListArray objectAtIndex:indexOfManager];
                    string  = [NSString stringWithFormat:@"identifier == \"%@\" ",deliveredMessage.identifier];

                    predicate = [NSPredicate predicateWithFormat:string];
                    filterArray = [managerDiscussionOject.replies.data filteredArrayUsingPredicate:predicate];

                    if ( filterArray.count != 0 ) {

                        NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init];

                        NSUInteger idx = 0;
                        for ( Message *message in managerDiscussionOject.replies.data ) {
                            if ([message.identifier isEqualToString:deliveredMessage.identifier] && [message.identifier isEqualToString:message.internalBaseClassIdentifier]){
                                [indexes addIndex:idx];

                            }
                            idx ++;
                        }

                        [managerDiscussionOject.replies.data removeObjectsAtIndexes:indexes ];

                        [[NSNotificationCenter defaultCenter]
                         postNotificationName:UPDATE_REPLY_ARRAY
                         object:managerDiscussionOject];

                    }


                                            //[DataManager serializeChatList];
                }
                //This case will be handle when time is same
            }
            else {

                /* NSString *string  = [NSString stringWithFormat:@"dataIdentifier == %@ ",sendMessage.disscussionId];

                 NSPredicate *predicate = [NSPredicate predicateWithFormat:string];

                 NSArray *filterArray = [[DataManager sharedManager].chatListArray filteredArrayUsingPredicate:predicate];

                 if (filterArray.count != 0 ) {

                 DiscussionData *discussionOject = [filterArray objectAtIndex:0];
                 NSUInteger indexOfManager = [[DataManager sharedManager].chatListArray indexOfObject:discussionOject];
                 DiscussionData *managerDiscussionOject = [[DataManager sharedManager].chatListArray objectAtIndex:indexOfManager];


                 [DataManager serializeChatList];
                 }*/

            }



        }else {
            NSLog(@"chat reply error === %@",error.localizedDescription);
            sendMessage.deliveryStatus = @"3";

            if ([sendMessage.disscussionId isEqualToString:disscussionId]) {

                if ( repliesArray.count != 0 ) {

                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self indexOfReplyId:sendMessage.internalBaseClassIdentifier] inSection:0];
                    //NSIndexPath *indexPath = [NSIndexPath indexPathForRow inSection:0];
                    ChatDetailBaseTVC *cell = (ChatDetailBaseTVC *)[self.tableViewChatConv cellForRowAtIndexPath:indexPath];
                    cell.msgDeliveryStatusImageView.image = [UIImage imageNamed:MESSAGE_RETRY_IMAGE];
                    cell.status = @"3";

                    if (sendMessage.attachment) {
                        [cell.fileNameLabel setTitle:[NSString stringWithFormat:@" %@", cell.messageObj.attachment.title ] forState:UIControlStateNormal];
                        cell.progressValue = 0.0 ;
                        sendMessage.attachment.progress = @"0%";
                    }
                    [self updateContentInsetForTableView:self.tableViewChatConv animated:NO];
                    [self addReplyInDiscussion:sendMessage.disscussionId];

                }
            }


        }
    }@catch(NSException *e) {}

} showSpinner:YES showProgressView:^(float progress, NSString *messageId) {

    [self updateCellProgress:progress :messageId :sendMessage];


}];
}



-(void)updateCellProgress:(float) progress :(NSString *)messageId :(Message*)sendMessage{

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self indexOfReplyId:messageId] inSection:0];
ChatDetailAttached *cell = (ChatDetailAttached *)[self.tableViewChatConv cellForRowAtIndexPath:indexPath];

cell.messageObj.attachment.progress =[NSString stringWithFormat:@"%.0f%%",progress];
NSLog(@"indx %ld == message id %@  progress == %@", (long)indexPath.row, messageId,cell.messageObj.attachment.progress);

if ([cell isKindOfClass:[ChatDetailAttached class]] &&
    [sendMessage.internalBaseClassIdentifier isEqualToString:cell.messageObj.internalBaseClassIdentifier]) {

    [cell.fileNameLabel setTitle:[NSString stringWithFormat:@"  %@ %@",cell.messageObj.attachment.progress, cell.messageObj.attachment.title ] forState:UIControlStateNormal];
}

}

2 个答案:

答案 0 :(得分:0)

尝试更新主线程中的单元格。当你使用block时使用block变量。

  1. 有一个变量来保持进度,CGFloat _progressNSIndexPath *uploadIndex
  2. 点击上传后,将单元格索引保存到uploadIndex。
  3. 更新进度变量
  4. 在showProgressView:block

    中重新加载单元格
            showProgressView:^(float progress, NSString *messageId) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    _progress = progress; [self.tableViewChatConv reloadRowsAtIndexPaths:@[uploadIndex] withRowAnimation:UITableViewRowAnimationNone];
                }
            }
    
  5. 在cellForRowAtIndex中,if([indexPath isEqual:uploadIndex])然后将进度更新为单元格cell.progressValue = _progress;

答案 1 :(得分:0)

在viewDidLoad中放置一个NSTimer,并在NSTimer的选择器上设置一个动作方法:

并在动作methode.g中重新加载表格视图:[_ mytableview reload];