2015-02-24 115 views
0

我的應用程序是一個假的孩子通訊應用程序。他們「發送消息」,然後繼續發送消息發送屏幕。當消息發送屏幕顯示時,它會觸發一個定時器,延遲創建的應答並將其添加到數組中,然後將使用該數組填充「收件箱」表。使用nslog,定時器運行,方法運行,數組被填充。但是,當我記錄行數(作爲array.count的結果)時,它記錄了0.我花了下午的時間試圖讓代碼工作,但我顯然錯過了一些東西。我甚至嘗試將創建數組的「邏輯代碼」移動到它自己的文件中,以將表和方法分開,但它達到了相同的結果。 問題是收件箱table.m中的respondArrayForTable返回null。但它會在收件箱中回覆方法,並且即使我在inboxTable中創建了一個收件箱實例,並通過視圖進行設置,加載並答覆ArraForTable = inboxInstance.allReplies,它仍會返回null。用於tableview返回的數組0

@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 
+1

對於這樣的問題,當'[array count] == 0'時,通常意味着'array == nil'。確保你正在分配它。 – trojanfoe 2015-02-24 15:19:54

回答

0

試試這個:

inboxTable.repliesArrayForTable = [replies mutableCopy]; //logs as should 

return allReplies = [replies mutableCopy]; 

如果不這樣做的伎倆再移動inboxTable例如repliesMethod