NSArray 在视图加载 3 次后清除 [英] NSArray clears after 3 times view did load

查看:51
本文介绍了NSArray 在视图加载 3 次后清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几个项目的 NSArray.NSArray 从另一个类加载到 uitableview 中.当我退出详细视图并重新输入(第三次)时,NSArray 为空,tableview 也为空.怎么了?(我使用的是 arc 所以我不认为这是泄漏)

I have an NSArray that contains a couple of items. The NSArray is loaded into a uitableview from another class. when I back out of the detail view and re-enter (for the 3rd time), the NSArray is empty and the tableview is empty as well. What is happening? (I am using arc so I don't think it is a leak)

- (void)viewDidLoad {

    myMatch = [[Match alloc] initWithId:1 OpponentId:13];
}


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

    static NSString *askCellIdent = @"askCell";
    static NSString *answerCellIdent = @"answerCell";


    if (indexPath.row == 0)
    {

        AskCell *cell = [tv dequeueReusableCellWithIdentifier:askCellIdent];
        if (cell==nil) {
            cell = [[AskCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:askCellIdent];
        }

        cell.nameLabel.text = @"Albert asked:";
        cell.questionLabel.text = [NSString stringWithFormat:@"%@", [[myMatch.chat objectAtIndex:indexPath.row] objectAtIndex:1]];
        return cell;
    }
    if (indexPath.row == 1)
    {

        AnswerCell *cell = [tv dequeueReusableCellWithIdentifier:answerCellIdent];
        if (cell==nil) {
            cell = [[AnswerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:answerCellIdent];
        }

        cell.nameLabel.text = @"Hugh answered:";
        cell.answerLabel.text = [NSString stringWithFormat:@"%@", [[myMatch.chat objectAtIndex:indexPath.row] objectAtIndex:1]];
        return cell;
    }

}

这是我的匹配类的初始化代码:

Here is the init code of the my match class:

- (id) initWithId:(NSInteger)yourId OpponentId:(NSInteger)opId {
    self = [super init];
    if (self != nil) {
        //set your id and opponents id to the match.
        [self setUserId:yourId];
        [self setUserId:opId];

        JSON = @"[{\"opponentId\":\"4\",\"chat\":[[\"1\",\"Does he have a beard?\"],[\"1\",\"No.\"]]}]";

        //initiate the chat array.
        chat = [[NSMutableArray alloc] init ];
        [self loadMatch];


    }
    return self;
}

这里是聊天属性/合成

NSMutableArray *chat;

@property (nonatomic, retain) NSMutableArray *chat;

@synthesize chat;

不知道怎么回事,因为数组的slog也是空的!

No idea whats going on, because the slog of the array is empty too!

推荐答案

在 appDelegate 中声明一个 int 变量 repeatCount

Declare an int varable repeatCount in appDelegate

- (void)viewDidLoad
{
    [super viewDidLoad];
    ypurAppDelegate *appDelegate = (yourAppDelegate *)[[UIApplication sharedApplication]   elegate];


          appDelegate.repeatCount++;

       if(appDelegate.repeatCount %3==0)
       {
           [array removeAllObjects];
            [tableview reloaddata];
        }
}

这篇关于NSArray 在视图加载 3 次后清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆