I have a UITableView which I populate using a NSMutableArray. This tableview updates when scrolled down, which is brought about by adding more data to the NSMutableArray. The problem I am facing is that everytime I navigate away from this page to another and then back again, the tableview is set to the initial size of the array, no matter how many updates I do (meaning if I load ten objects each time, the tableview size reverts back to 10 even if the array size is 30, note: the array size never changes only the table content size does). I am starting to believe this has to do with the properties of the NSMutableArray. The gist of the code is this:
@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
I hope that is not too much. Please tell me where I might be going wrong.
As I couldn’t find what is going wrong in my code, I have made a temporary fix. Like I said, the NSMutableArray cellData is not being changed, but the only thing that is changing is UITableView itself (I found this in my viewDidDisappear method). So I just did this to compensate for this
This returns the flowTable to the last selected cell. It isn’t elegant, but it works. Please do keep posting your ideas. I really want to get to the bottom of this.