UITableView在滚动时重新加载数据

PAc*_*can 0 objective-c uitableview mkmapview ios

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

我正在将一些数据下载到webview并在地图上显示一些注释.问题是,当用户更改方向并滚动tableview数据时,表视图会自动重新加载数据(我的webview和Map正在重新创建并且不显示任何内容).如何解决这个问题?也许我可以保存tableviewcell的状态?? 但怎么办呢?

Rei*_*ner 7

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

  • 这次是1000.如果我可以投票五次,我会.人们一直在视图中存储数据.不好. (2认同)