Sunday, 15 September 2013

UITableView reloads data while scrolling

UITableView reloads data while scrolling

I've create an app that uses some maps functionality. I've added an
MKMapView to the UITableViewCell and WebView to another uiTableviewcell (I
need this, because it look elegant). I've created my custom cells. My
uitableview delegate and datasource methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger section = indexPath.section;
switch (section)
{
case 0:
return [self cellForMapView];
case 1:
return [self cellForUIWebView];
}
return nil;
}
-(UITableViewCell *)cellForMapView
{
//if (_mapViewCell)
// return _mapViewCell;
// if not cached, setup the map view...
CGFloat cellWidth = self.view.bounds.size.width - 20;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
cellWidth = self.view.bounds.size.width - 90;
}
CGRect frame = CGRectMake(0, 0, cellWidth, 241);
_mapView = [[MKMapView alloc] initWithFrame:frame];
_mapView.showsUserLocation = YES;
_mapView.userLocation.title = @"Òåêóùåå ìåñòîïîëîæåíèå";
_mapView.mapType = MKMapTypeStandard;
_mapView.zoomEnabled = YES;
NSString * cellID = @"Cell";
UITableViewCell *cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID]
autorelease];
[cell.contentView addSubview:_mapView];
_mapViewCell = cell;
return cell;
}
/*
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50.0f;
} */
-(UITableViewCell *)cellForUIWebView
{
//if (_webViewCell)
// return _webViewCell;
CGFloat cellWidth = self.view.bounds.size.width - 20 ;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
cellWidth = self.view.bounds.size.width - 90;
}
CGRect frame = CGRectMake(0, 0, cellWidth, 241);
_webView = [[UIWebView alloc] initWithFrame:frame];
NSString * cellID = @"Cell";
UITableViewCell *cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:cellID]autorelease];
[cell.contentView addSubview:_webView];
_webViewCell = cell;
return cell;
}
I'm downloading some data to the webview and to display some annotations
on the map. The problem is that when the user is changing orientation and
scrolling the tableview data, table view reloads the data automatically (I
have my webview and Map being recreated and displaying nothing). How to
fix this problem? Maybe I can save the tableviewcell's state?? But how to
do it?

No comments:

Post a Comment