我只是被困在这里,我不知道我在这里做错了什么。在我的应用程序中,我使用UITableView和NSMutableArray解析来自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移出)。‘*第一次抛出调用堆栈:(0x2e332e83 0x3868f6c7 0x2dd55 0x2ecdb0x30c34d 0x30cb083f 0x6d2d9 0x30bda3 0x30bda3 0x30baebd3f 0x30baedd3f 0x30daedod3 0x30daed13 0x30x37ad0x30bx7cx30cx30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30b30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x6x30x30x30x6x30x30x630x30x30x30x30x6x30x30x6x30x30x30x6x30x30x30x30x30x6x30x30xbd3/0x30x30x30x30x30x30x6x30x30x30x30x6x30x30x30x30x30x6x30x30x30x30x30x30x30x30x30x630x30x30x30x630x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30x30dbd30x30x30x30x30x30d30
任何帮助都非常感谢!
发布于 2013-11-07 07:00:27
更新tableview的源数组,不需要插入新行。
[self.earthquakeList addObjectsFromArray:earthquakes];
[self.tableView reloadData];发布于 2013-11-07 07:09:29
如果将元素添加到数据源数组中,则为否。表视图中的行将自动更新。您不需要插入行。只是插入元素。将您的方法实现为:
- (void)addEarthquakesToList:(NSArray *)earthquakes
{
[self.earthquakeList addObjectsFromArray:earthquakes];
[self.tableview reloadData];
} 发布于 2013-11-07 09:52:25
呃,而不是
[self.tableView insertRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationAutomatic];试一试
[self.tableView reloadRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationAutomatic];不管怎样..。至少对我来说,您的代码片段仍然不足以找到您的问题。
但既然你说:
I want to refresh the whole table with new data when user click on the segment control in the tableview.
如果我必须用4个段来显示不同段的不同数据的分段tableView,我会这样做:
注:这是一个基本的例子。它不遵循特定的代码,但应该让您放弃使用indexPaths或手动从数组中添加/删除对象的逻辑,而应该适当地使用tableView reloadData。
- (void)viewDidLoad
{
[super viewDidLoad];
//declare NSArray *arrFirst, *arrSecond, *arrThird, *arrFourth, *arrSelected
//in the .h file
arrFirst = [[NSArray alloc] initWithObjects:@"eins",@"zwei",@"drei",@"vier",@"fünf",@"sechs", nil];
arrSecond = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four",@"five",@"six",@"seven",@"eigth",@"nine", nil];
arrThird = [[NSArray alloc] initWithObjects:@"uno",@"dos",@"tres",@"cuatro",@"cinco", nil];
arrFourth = [[NSArray alloc] initWithObjects:@"ek",@"do",@"teen",@"chaar",@"paach",@"che",@"saat", nil];
arrSelected = arrFirst;
}- (IBAction)mySegmentsAct:(UISegmentedControl *)sender
{
switch (sender.selectedSegmentIndex)
{
case 0:
arrSelected = arrFirst;
break;
case 1:
arrSelected = arrSecond;
break;
case 2:
arrSelected = arrThird;
break;
case 3:
arrSelected = arrFourth;
break;
default:
arrSelected = nil;
break;
}
[myTableView reloadData];
}-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [arrSelected count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = (arrSelected)[indexPath.row];
return cell;
}我也可以使用单个数组来完成这一任务,方法是首先刷新旧的数组内容:
arrSelected = nil
然后将新元素放入这个数组中(使用某种逻辑从某个源)
最后:
myTableView reloadData。
https://stackoverflow.com/questions/19828419
复制相似问题