我正在尝试使用 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/