gpt4 book ai didi

ios - UITableView 在滚动时重新加载数据

转载 作者:行者123 更新时间:2023-11-28 18:06:03 25 4
gpt4 key购买 nike

我创建了一个使用某些 map 功能的应用程序。我已经将 MKMapView 添加到 UITableViewCell,将 WebView 添加到另一个 uiTableviewcell(我需要这个,因为它看起来很优雅)。我已经创建了自定义单元格。我的 uitableview 委托(delegate)和数据源方法:

- (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;
}

我正在将一些数据下载到 webview 并在 map 上显示一些注释。问题是,当用户改变方向并滚动表格 View 数据时,表格 View 会自动重新加载数据(我有我的 webview 和 map 被重新创建并且什么都不显示)。如何解决这个问题?也许我可以保存 tableviewcell 的状态??但是要怎么做呢?

最佳答案

问题是您试图将数据存储在表格 View 单元格中,但这些数据是由表格 View 管理的。它们不仅会根据需要重新加载,还会在它们从屏幕上消失时重新使用。
解决方案是您必须将数据存储在数据模型中,这是一个独立于 TableView 的对象。表格 View 然后仅从您的模型加载数据以供显示。

关于ios - UITableView 在滚动时重新加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18820599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com