2013-01-14 191 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没有工作我害怕。:( –