ios - UITableViewCell 中的行仅显示来自 USGS 的第一个索引数组

标签 ios objective-c uitableview

我正在尝试使用 AFNetworking 框架从 USGS 地震中获取数据,并过滤区域为韩国。我在方法 tableView cellForRowAtIndexPath 中显示数据时遇到问题。 tableview 中的行数与数据地震的数量相同,但问题是为什么数据只显示韩国的第一次地震。

这是我的方法 tableView cellForRowAtIndexPath 的代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EarthquakeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    NSArray *earthquakeFeaturesData = [self.earthquakeDataService features];

    int indexEarthquake = 0;
    for ( NSDictionary *earthquakeData in earthquakeFeaturesData){
        if ([[earthquakeData title] containsString:@"California"]){
            NSString *stringDate = [Helper convertMilisecondsToReadableStringDate:[earthquakeData updated]];
            cell.placeLabel.font = [UIFont fontWithName:[NSString stringWithFormat:@"%@", GLOBAL_FONT_TYPE] size:14];
            cell.placeLabel.text =[earthquakeData place];
            cell.magLabel.font = [UIFont fontWithName:[NSString stringWithFormat:@"%@", GLOBAL_FONT_TYPE] size:25];
            cell.magLabel.text = [NSString stringWithFormat:@"%@", earthquakeData.mag];
            cell.mLabel.font = [UIFont fontWithName:[NSString stringWithFormat:@"%@", GLOBAL_FONT_TYPE] size:13];
            cell.mLabel.text = [NSString stringWithFormat:@"Magnitude"];
            cell.timeLabel.font = [UIFont fontWithName:[NSString stringWithFormat:@"%@", GLOBAL_FONT_TYPE] size:14];
            cell.timeLabel.text = [NSString stringWithFormat:@"%@ ", stringDate];

            NSLog(@"%@", earthquakeData.mag);
            indexEarthquake++;
            NSLog(@"%i", indexEarthquake)
        }
    }

    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(!self.earthquakeDataService)
        return 0;
    else{
        NSArray *earthquakeFeaturesData = [self.earthquakeDataService features];
        int indexEarthquake = 0;
        for ( NSDictionary *earthquakeData in earthquakeFeaturesData){
            if ([[earthquakeData title] containsString:@"California"]){
                indexEarthquake++;
            }
        }
        NSLog(@"%i",indexEarthquake);
        return indexEarthquake;
    }
}

我认为问题是因为我需要数组来保存来自 USGS 的临时数据并在迭代后呈现它。那么,如何解决这个问题??

最佳答案

cellForRowAtIndexPath 会为 TableView 中的每个单元格调用一次。每次调用它时,您都会执行相同的操作,从索引 0 开始并迭代数据。因此,您的所有单元格都包含相同的数据。您应该使用 indexPath 来选择要使用的数据。如果您在加载到 tableview 之前开始过滤数据而不是尝试在运行中这样做,这可能会更容易。

关于ios - UITableViewCell 中的行仅显示来自 USGS 的第一个索引数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27486973/

相关文章:

ios - 如何在旋转时处理 UIView 圆角(使用 CAShapeLayer)

ios - swift 4中导航栏的高度

ios - 快速更改另一个类的变量值

ios - UIscrollview 中的自定义 UIButton

iOS7 UIPickerView/UIScrollView

ios - UITableView 不重复使用 Storyboard的单元格

ios - 什么逻辑用于创建均衡器表

ios - 如何使用 UIView 创建自定义翻转动画

ios - dispatch_queue_t 定义为强

ios - 不符合 UITableViewDataSource - 解析应用