我的应用是针对孩子的假消息应用。他们“发送消息”,然后发送到消息发送屏幕。当消息发送屏幕显示时,它会触发一个计时器,该计时器会延迟创建的回复并添加到数组中,然后将用于填充“收件箱”表。使用nslog,计时器运行,方法运行,数组被填充。但是,当我记录行数(作为array.count的结果)时,它记录为0.我花了整个下午试图让代码工作,但我显然错过了一些东西。我甚至尝试将用于创建数组的“逻辑代码”移动到它自己的文件中以分离它和表方法,但它实现了相同的结果。 问题是inbox table.m中的repliesArrayForTable返回null。但是它会在收件箱repliesMethod中记录名称,即使我在inboxTable中创建了一个收件箱实例,并通过视图设置它也加载并且repliesArrayForTable = inboxInstance.allReplies它仍然返回null。
@implementation inbox
@synthesize allReplies, childsName;
-(void)timerForOtherMethods {
NSLog(@"populate array timer");
NSTimer *timer = [NSTimer timerWithTimeInterval:6 target:self selector:@selector(repliesMethod) userInfo:nil repeats:NO];
NSLog(@" add timer");
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
}
-(NSMutableArray *)repliesMethod {
NSMutableArray *replies = [[NSMutableArray alloc]init];
[replies addObject:childsName]; // childsName set in messageSent
NSLog(@" replies array includes: %@", replies); //logs name
InboxTable *inboxTable = [[InboxTable alloc]init];
inboxTable.repliesArrayForTable = replies; //logs as should
return allReplies = replies;
}
@implementation InboxTable
static NSString *CellIdentifier = @"Cell Identifier";
@synthesize repliesArrayForTable;
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated {
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"replies in array: %lu", (unsigned long)repliesArrayForTable.count); // logs 0?
return [repliesArrayForTable count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = @"From Sorting Office";
return cell;
}
@end
答案 0 :(得分:0)
试试这个:
inboxTable.repliesArrayForTable = [replies mutableCopy]; //logs as should
return allReplies = [replies mutableCopy];
如果这不能解决问题,那么移动你的 inboxTable 实例 你的 repliesMethod 。