-1

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

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

}

这是我的比赛类的初始化代码:

- (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 也是空的!

4

4 回答 4

0

使用 self.chat = [[[NSMutableArray alloc] init ]autorelease]; 初始化你的数组

于 2012-05-02T06:22:04.097 回答
0

取而代之的是 ViewDidLoad,在 initWithCoder 中分配对象。

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

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if( self )
    {
       myMatch = [[Match alloc] initWithId:1 OpponentId:13];
    }
return self;
}
于 2012-05-02T06:43:23.617 回答
0

在 appDelegate 中声明一个 int 变量 repeatCount

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


          appDelegate.repeatCount++;

       if(appDelegate.repeatCount %3==0)
       {
           [array removeAllObjects];
            [tableview reloaddata];
        }
}
于 2012-05-02T07:00:50.650 回答
0

我认为这是填充的问题,NSMutableArray因为只会viewDidLoad调用一次。viewWillAppear您应该在Method 中将值插入数组或在initWithCoder.

于 2012-05-02T07:39:41.613 回答