NSArray clears after 3 times view did load

只愿长相守 提交于 2019-12-13 22:17:35

问题


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;
}

and here is the chat property/synthesize

NSMutableArray *chat;

@property (nonatomic, retain) NSMutableArray *chat;

@synthesize chat;

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


回答1:


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];
        }
}



回答2:


Use self.chat = [[[NSMutableArray alloc] init ]autorelease]; to initialize your array




回答3:


Instead ViewDidLoad, allocate the object in initWithCoder.

- (void)viewDidLoad
{
   [super viewDidLoad];
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if( self )
    {
       myMatch = [[Match alloc] initWithId:1 OpponentId:13];
    }
return self;
}



回答4:


I think it's a problem with the populating the NSMutableArray as only once will viewDidLoad will be called. You should insert values into array in viewWillAppear Method or initiate the match class in initWithCoder.



来源:https://stackoverflow.com/questions/10408584/nsarray-clears-after-3-times-view-did-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!