我有一个包含几个项目的NSArray。 NSArray从另一个类加载到uitableview中。当我退出详细视图并重新进入(第3次)时,NSArray为空,tableview也是空的。怎么了? (我正在使用arc,所以我不认为这是泄漏)
- (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;
不知道发生了什么事,因为阵列的混乱也是空的!
答案 0 :(得分:0)
使用self.chat = [[[NSMutableArray alloc] init] autorelease];初始化你的数组
答案 1 :(得分: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;
}
答案 2 :(得分:0)
在appDelegate中声明一个int varable repeatCount
- (void)viewDidLoad
{
[super viewDidLoad];
ypurAppDelegate *appDelegate = (yourAppDelegate *)[[UIApplication sharedApplication] elegate];
appDelegate.repeatCount++;
if(appDelegate.repeatCount %3==0)
{
[array removeAllObjects];
[tableview reloaddata];
}
}
答案 3 :(得分:0)
我认为填充NSMutableArray
是一个问题,因为只会调用viewDidLoad
一次viewWillAppear
。您应该在initWithCoder
方法中将值插入数组,或在{{1}}中启动匹配类。