一段时间后,Array丢失了它的对象

时间:2016-08-31 09:39:59

标签: ios objective-c nsmutablearray xmpp openfire

我创建了聊天应用,我将消息添加到arrayChat

-(void)msgRecevied:(NSMutableDictionary *)messageContent
{
    NSString *m = [messageContent objectForKey:kMsg];
    [messageContent setObject:[m substituteEmoticons] forKey:kMsg];
    self.arrayTemp = messageContent;

    [self.arrayChat addObject:self.arrayTemp];

    [_tblChat reloadData];

    if(self.arrayChat.count > 1){

        NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:self.arrayChat.count-1
                                                       inSection:0];

        [self.tblChat scrollToRowAtIndexPath:topIndexPath
                            atScrollPosition:UITableViewScrollPositionMiddle
                                    animated:YES];
    }
}

但是经过一段时间我把我的设备放了一段时间之后,当它发送消息之后,它将对象添加到arrayChat但它没有加载到tableView中,我重新加载表它显示了它丢失的最后一个对象来自arrayChat

我声明arrayChat喜欢:

@interface ViewController : UIViewController
{

    NSMutableArray *arrayChat;
}
@property (strong,nonatomic) NSMutableArray *arrayChat;

我也@synthesize arrayTemp;

这里有什么错误吗?

修改

我将arrayChat添加到其他类,我将该类声明为Appdelegate我使用该类访问应用程序上的该数组,并且它正在工作,但是当我添加时会出现问题对象到数组,但它没有立即重新加载我的表,我在按钮上放大了我的聊天视图,那时我最后添加的对象是数组。

这是我放大视图时的代码:

-(IBAction)onClickViewLarge:(id)sender{

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth  = screenRect.size.width;
    CGFloat screenHeight  = screenRect.size.height;


    if (!isShow) {

        [_btnOpen setImage:[UIImage imageNamed:@"ico_expand"] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.5 animations:^{

            if(screenHeight >= 736)
                self.view.frame = CGRectMake(0,66 , screenWidth, screenHeight - 66);
            else if (screenHeight >= 667)
                self.view.frame = CGRectMake(0,60 , screenWidth, screenHeight - 60);
            else if (screenHeight >= 568)
                self.view.frame = CGRectMake(0,51 , screenWidth, screenHeight - 51);
            else if (screenHeight >= 480)
                self.view.frame = CGRectMake(0,43 , screenWidth, screenHeight - 43);
        }];
        isShow = true;

    } else {
        [_btnOpen setImage:[UIImage imageNamed:@"ico_open"] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.5 animations:^{
            self.view.frame = CGRectMake(screenWidth - 250,screenHeight - 450 , 250, 450);
        }];
        isShow = false;
    }

    if([app.Glb.arrayChat count] > 0)
    {
        [self.tblChat reloadData];

        NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:app.Glb.arrayChat.count-1
                                                       inSection:0];

        [self.tblChat scrollToRowAtIndexPath:topIndexPath
                            atScrollPosition:UITableViewScrollPositionMiddle
                                    animated:YES];
    }

}

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我认为问题出在全局声明的arrayTemp对象上。每次你覆盖它的值,所以你要覆盖arrayChat本身的内容。尝试将此方法声明为您的方法本地

-(void)msgRecevied:(NSMutableDictionary *)messageContent
{
    NSString *m = [messageContent objectForKey:kMsg];
    [messageContent setObject:[m substituteEmoticons] forKey:kMsg];
    NSMutableDictionary *arrayTemp = [messageContent mutableCopy];

    [self.arrayChat addObject:arrayTemp];

    [_tblChat reloadData];

    if(self.arrayChat.count > 1){

        NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:self.arrayChat.count-1
                                                       inSection:0];

        [self.tblChat scrollToRowAtIndexPath:topIndexPath
                            atScrollPosition:UITableViewScrollPositionMiddle
                                    animated:YES];
    }
}