我只是被困在这里,我无法弄清楚我在这里做错了什么。我对目标 c 很陌生。在我的应用程序中,我使用UITableView
andNSMutableArray
来解析来自 RSS 提要的数据。当用户单击表格视图中的段控件时,我想用新数据刷新整个表格。我在表头部分添加了段控件,它可以在段索引更改时更改值。
现在的问题是,当我单击段控件时,它会在表格视图中添加新行,而不是刷新整个表格。这是代码:
- (void)addEarthquakesToList:(NSArray *)earthquakes
{
//NSInteger startingRow = [self.earthquakeList count];
NSInteger earthquakeCount = [earthquakes count];
NSMutableArray *indexPaths = [[NSMutableArray alloc] initWithCapacity:earthquakeCount];
NSLog(@"%d",earthquakeCount);
for (NSInteger row = 0; row < (earthquakeCount); row++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
[indexPaths addObject:indexPath];
}
[self.earthquakeList addObjectsFromArray:earthquakes];
[self.tableView insertRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationAutomatic];
}
我尝试了许多其他方法,例如[self.tableView reloadData];
我还尝试了以下方法从数组中删除所有对象并从表中删除行,但它没有显示正确的行数:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
for(unsigned int i = 0; i < [self.earthquakeList count]; i++)
{
[indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
//[self.tableView beginUpdates];
[self.earthquakeList removeAllObjects];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete
withRowAnimation:UITableViewRowAnimationFade];
//[self.tableView endUpdates];
//[self.tableView reloadData];
}
这些是我的 tableView DataSource 方法:
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
// The number of rows is equal to the number of earthquakes in the array.
return [self.earthquakeList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kEarthquakeCellID = @"EarthquakeCellID";
citsTableViewCell *cell = (citsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kEarthquakeCellID];
//Get the specific earthquake for this row.
citsFuelFinder *earthquake = (self.earthquakeList)[indexPath.row];
[cell configureWithEarthquake:earthquake];
return cell;
}
事实上,当尝试在段控制中包含以下内容时:
- (void)segmentedControlHasChangedValue
{
int product;
product = fuelType.selectedSegmentIndex;
switch (product)
{
case 0:
productName=1;
[locationManager startUpdatingLocation];
break;
case 1:
productName=2;
//[self.earthquakeList removeLastObject];
if (![self.earthquakeList count])
{
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:0]
withRowAnimation:UITableViewRowAnimationRight];
}
else
{
NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
for(unsigned int i = 0; i < [self.earthquakeList count]; i++)
{
[indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i
inSection:0]];
}
//[self.tableView beginUpdates];
//[self.earthquakeList removeAllObjects];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete
withRowAnimation:UITableViewRowAnimationFade];
//[self.tableView endUpdates];
[self.tableView reloadData];
//[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
[locationManager startUpdatingLocation];
break;
case 2:
productName=4;
[locationManager startUpdatingLocation];
break;
case 3:
productName=5;
[locationManager startUpdatingLocation];
break;
default:
break;
}
}
我收到此错误:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (15) 必须等于该节中包含的行数更新前的节 (15),加上或减去从该节插入或删除的行数(0 插入,15 删除),加上或减去移入或移出该节的行数(0 移入,0 移动出去)。' *First throw call stack: (0x2e332e83 0x3868f6c7 0x2e332d55 0x2ecdb0af 0x30c8a34d 0x30cb083f 0x6d2d9 0x30aebda3 0x30aebd3f 0x30aebd13 0x30ad7743 0x30bf7c59 0x30bf7a27 0x30cbca25 0x30aaf1a1 0x30ae69fd 0x30ae63ab 0x30abbd79 0x30aba569 0x2e2fdf1f 0x2e2fd3e7 0x2e2fbbd7 0x2e266471 0x2e266253 0x32fa02eb 0x30b1b845 0x6948d 0x38b88ab7)
非常感谢任何帮助!