2013-01-14 114 views
1

我有一個UITableView我填充使用NSMutableArray。此向下滾動時會更新這個tableview,這是通過將更多數據添加到NSMutableArray而實現的。我面臨的問題是,每次我從這個頁面導航到另一個頁面,然後再返回,tableview被設置爲數組的初始大小,無論我做了多少更新(意思是說,如果我每次加載10個對象,即使數組大小爲30,tableview大小也會恢復爲10,注意:數組大小決不會僅更改表內容大小)。我開始相信這與NSMutableArray的屬性有關。代碼的要點是這樣的:NSMutableArray屬性來填充UITableView

@interface FlowViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 
{ 
    UITableView *flowTable; 

    NSMutableArray *cellData; 

} 
@property(nonatomic, retain) IBOutlet UITableView *flowTable; 

@property(nonatomic, retain) NSMutableArray *cellData; 

- (void) getData; 
- (void) storeData: (NSMutableArray*) arr; 

@end 

@implementation FlowViewController 

@synthesize cellData; 

@synthesize flowTable; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.flowTable.autoresizingMask = UIViewAutoresizingFlexibleHeight; 

    [self.flowTable setDataSource:self]; 
    [self.flowTable setDelegate:self]; 

    self.cellData = [[NSMutableArray alloc] init]; 

    [self getData]; 
} 

- (void) storeData:(NSMutableArray *)arr 
{ 
    for(NSDictionary *data in arr) 
    { 

     CellObject *det = [[CellObject alloc] init]; 

     // store details 

     [self.cellData addObject: det]; 

    } 

    [self.flowTable reloadData]; 
} 

- (void) getData 
{ 

    NSString *url = @"http://example.com/"; 

    NSMutableURLRequest *theRequest= [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy 
                timeoutInterval:10.0]; 

[theRequest setHTTPMethod:@"GET"]; 

flowConnection =[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES]; 

} 


#pragma mark - 
#pragma mark Table View Data Source Methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    return [self.cellData count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *CellIdentifier = @"FlowCell"; 

    MyFlowCell *cell = (MyFlowCell *)[self.flowTable dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FlowCell" owner:nil options:nil]; 
     // cell = [nib objectAtIndex:0]; 

     for(id currentObject in nib) 
     { 

     if([currentObject isKindOfClass:[MyFlowCell class]]) 

     { 
      cell = (MyFlowCell *)currentObject; 

     break; 
    } 
    } 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

} 

CellObject *rowItem = [cellData objectAtIndex:indexPath.row]; 

    // set cell data 

} 

    return cell; 
} 


- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    self.current = indexPath.row; 

    [self performSegueWithIdentifier:@"flowToAnotherSegue" sender:nil]; 

} 


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 

    if ([segue.identifier isEqualToString:@"flowToAnotherSegue"]) 
    { 
     NewViewController *iv = 
      segue.destinationViewController; 

    iv.current = self.current; 
     iv.data = self.cellData; 

    } 

} 


#pragma mark - 
#pragma NSURLConnection Delegate Methods 
- (void) connection:(NSURLConnection *)_connection didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"Receiving response: %@, status %d", [(NSHTTPURLResponse*)response allHeaderFields],  [(NSHTTPURLResponse*) response statusCode]); 
    receivedData = [NSMutableData data]; 
} 

- (void) connection:(NSURLConnection *)_connection didFailWithError:(NSError *)error { 
    NSLog(@"Connection Failed: %@", error); 
} 

- (void) connection:(NSURLConnection *)_connection didReceiveData:(NSData *)_data { 

     [receivedData appendData:_data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 

    // get the new NSMutableArray from receivedData 

    [self storeData: newMutableArray]; 

} 


#pragma mark - 
#pragma mark Deferred image loading (UIScrollViewDelegate) 

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView { 

    if(Bottom reached) { 

     // load more data 

     [self getData]; 

     } 

    } 
} 

@end 

我希望不是太多。請告訴我哪裏可能會出錯。

回答

0

,因爲我無法找到是什麼在我的代碼去錯了,我做了一個臨時的解決辦法。就像我說的,NSMutableArray cellData沒有被改變,但唯一改變的是UITableView本身(我在我的viewDidDisappear方法中發現了這個)。所以我只是這樣做以彌補這一點

- (void) viewDidAppear:(BOOL)animated 
{ 
    [self.flowTable reloadData]; 

    [self.flowTable scrollToRowAtIndexPath: lastIndex atScrollPosition:0 animated: NO]; 

} 

這將返回到最後選擇的單元格的flowTable。它不是優雅的,但它的作品。請繼續發佈您的想法。我真的很想深究這一點。

1

將數組存儲在共享類/單例類中。

由於每當你重新加載這個頁面,viewDidLoad被調用,你的數組是alloc + init再次完成,將其設置爲默認值。

+0

使數組「靜態」是一個糟糕的出路,幾乎總是錯誤的答案。該數組確實需要被定義爲一個繼續存在的類中的ivar。 – zaph

+0

就像我說過的,數組的大小保持不變,只有UITableView的大小發生變化。我用NSLog語句來確認這一點。 –

+0

@Zaph,我需要爲cellData ivar設置一個屬性(nonatomic,retain)嗎? –

0

如果刪除self.cellData = [[NSMutableArray alloc] init];從視圖中沒有負載,並用JIT方法創建自己的getter:

-(NSMutableArray *)cellData 
{ 
    if(!_cellData){ 
     _cellData = [NSMutableArray array]; 
    } 
    return _cellData; 
} 

cellData的內容將被保留,直到流量控制器被釋放。

+0

嗯。這沒有奏效。由於某種原因,執行沒有超過if語句。 –

0

您的數組正在viewDidLoad上重新創建,您可以在分配之前檢查它是否已經存在。

if (!self.cellData) { 
    self.cellData = [[NSMutableArray alloc] init]; 
    [self getData]; 
} 
+0

如果是這樣的話,那麼我的viewDidLoad方法中的NSLog語句不應該被調用嗎?此外,UITableView中的這種變化發生在viewDidDisappear方法(我沒有在這裏顯示)之前。所以,變化發生在「tableViewdidSelectRowAtIndexPath」和「viewDidDisappear」之間。 –

+0

在這種情況下,您在新視圖中如何處理iv.data? –

+0

在另一個滾動視圖中使用它。但我認爲這不會以任何方式影響它。我有一個不同的按鈕從頭字段不會傳遞任何數據的另一個segue。即使從該視圖返回,也會出現此問題。 –

1

將數組存儲在共享類/單例類中。您可以創建自己的共享課程,也可以使用appDelegate

  • 聲明appDelegate中的NSMutableArray屬性。 @property (nonatomic, strong) NSMutableArray *cellData; 併合成它。使用此數組而不是您的cellData數組。 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];更換您self.cellDataappDelegate.cellData

+0

在AppDelegate的ivars沒有工作我害怕。:( –