1

这是我的问题。我有一个根据数组内容填充的表格视图。到目前为止,一切都很好。该数组用于存储来自服务器的一些地图名称。我有一个刷新按钮,因此当我单击它时,它将使用新的地图名称(refresh方法)更新数组。

当我单击刷新按钮时,地图数组被修改。添加或删除某些地图(例如,如果用户删除了地图或创建了新地图)。但是,这种情况并没有反映在屏幕上。

假设我有 5 张地图(名为 1、2、3、4、5)。如果我删除一个(比如说地图 3)并调用刷新,地图数组 ( mapsModel->mapsNameList) 将包含 4 个地图,并且该数组的内容是正确的。但是,在 iphone 屏幕上,我会在表格视图中看到 (1,2,4,5,5)。我不知道为什么如果它不在 maps 数组中它不会删除一行。

如果我尝试添加地图(比如说地图 0),我会遇到同样的问题,我会得到 (0,1,2,3,4) 并且数字 5 不存在。

如果我重新启动应用程序,那么所有地图都会正确显示...

这是我的代码,如果某些变量名称不清楚,请告诉我!

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int numberOfRows = [[MapsModel sharedMapsModel] numberOfMapsInSection:((UITabBarController*) self.parentViewController).tabBar.selectedItem.tag];

    // Return the number of rows in the section. THIS FUNCTION WORKS
    return numberOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    MapsModel* mapsModel = [MapsModel sharedMapsModel];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    [[cell textLabel] setText:[[mapsModel->mapsNameList objectAtIndex:((UITabBarController*) self.parentViewController).tabBar.selectedItem.tag] objectAtIndex:indexPath.row]];

    return cell;
}


-(void) refresh {
   [[MapsModel sharedMapsModel] populateMapsNameListWithMapState:MAP_STATE_ALL];

    [self updateView];
}

-(void) updateView {
    //Reorder the maps by alphabetical order
    NSSortDescriptor *sortDescriptor;
    MapsModel* mapsModel = [MapsModel sharedMapsModel];

    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];

    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    [[mapsModel->mapsNameList objectAtIndex:self.tabBarItem.tag] sortUsingDescriptors:sortDescriptors];

    //Update the table view
    [self.tableView reloadData];
}
4

2 回答 2

0

您确定[[MapsModel sharedMapsModel] numberOfMapsInSection:更新后返回正确的值吗?您的表格代码没有问题,看来您应该检查您的MapsModel. 可能您的 mapsModel 在cellForRowAtIndexPath:numberOfRowsInSection:

于 2013-02-21T07:44:13.790 回答
0

我知道问题所在,我有一个标签栏控制器和三个表格视图。但是,我尝试对所有三个表视图只使用一个视图控制器......每个表视图一个视图控制器解决了我的问题!

于 2013-02-21T08:55:11.267 回答