当我点击第一部分时出现以下错误:
Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:1076
2014-02-26 14:12:38.935 ExpandableTableCells[1865:a0b]
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 8 from section 0 which only contains 1 rows before the update'
给我解决方案...... 谢谢... 代码:
#pragma mark - Expanding
- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
{
if (section>=0) return YES;
return NO;
//return YES;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"------->%d",section);
if (section==0)
{
if ([expandedSections containsIndex:section])
{
// return 5; // return rows when expanded
NSLog(@"%d",[self.alpha count]);
return [self.alpha count];
}
return 1;
}
if (section==1) {
if ([expandedSections containsIndex:section])
{
// return 5; // return rows when expanded
NSLog(@"vvv%d",[self.numbers count]);
return [self.numbers count];
}
return 1;
}
if (section==2)
{
if ([expandedSections containsIndex:section])
{
// return 5; // return rows when expanded
NSLog(@"%d",[self.colors count]);
return [self.colors count];
}
return 1;
}
return YES;
}
- (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];
}
// Configure the cell...
if (indexPath.section==0) {
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
// first row
cell.textLabel.text = @"Alaphabet"; // only top row showing
if ([expandedSections containsIndex:indexPath.section])
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
else
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
}
else
{
// all other rows
cell.textLabel.text = [self.alpha objectAtIndex:indexPath.row];
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
}
if (indexPath.section==1) {
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
// first row
cell.textLabel.text = @"numbers"; // only top row showing
if ([expandedSections containsIndex:indexPath.section])
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
else
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
}
else
{
// all other rows
cell.textLabel.text = [self.numbers objectAtIndex:indexPath.row];
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
}
if (indexPath.section==2) {
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
// first row
cell.textLabel.text = @"colors"; // only top row showing
if ([expandedSections containsIndex:indexPath.section])
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
else
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
}
else
{
// all other rows
cell.textLabel.text = [self.colors objectAtIndex:indexPath.row];
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
[self.tableView beginUpdates];
// only first row toggles exapand/collapse
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
NSLog(@"section------%d",section);
BOOL currentlyExpanded = [expandedSections containsIndex:section];
NSLog(@"current expand------%d",currentlyExpanded);
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
}
else
{
[expandedSections addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
NSLog(@"rows------%d",rows);
}
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
[tmpArray addObject:tmpIndexPath];
NSLog(@"temp array----->%@",tmpArray);
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (currentlyExpanded)
{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
else
{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
NSLog(@"open index------%d",openSectionIndex);
if (openSectionIndex != -1)
{
[tmpArray removeAllObjects];
rows = [self tableView:tableView numberOfRowsInSection:openSectionIndex];
[expandedSections removeIndex:openSectionIndex];
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:openSectionIndex];
[tmpArray addObject:tmpIndexPath];
}
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
}
openSectionIndex=section;
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
[self.tableView endUpdates];
}
}
}
答案 0 :(得分:1)
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
int selectedIndex;
UITableView *menuTableView;
NSMutableArray *colorsArray, *numberArray, *alphaArray ;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
selectedIndex=50;
colorsArray =[[NSMutableArray alloc]initWithObjects:@"red",@"yellow",@"pink",@"none", nil];
numberArray =[[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];
alphaArray =[[NSMutableArray alloc]initWithObjects:@"a",@"b",@"c",@"d",@"e", nil];
menuTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 60, 320, 404) style:UITableViewStyleGrouped];
menuTableView.delegate=self;
menuTableView.dataSource=self;
menuTableView.separatorColor=[UIColor grayColor];
[self.view addSubview:menuTableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count=0;
if (section ==selectedIndex) {
switch (section) {
case 0:
count= colorsArray.count;
break;
case 1:
count= numberArray.count;
break;
case 2:
count= alphaArray.count;
break;
default:
break;
}
}
else
{
count= 1;
}
return count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
switch (indexPath.section) {
case 0:
cell.textLabel.text =colorsArray[indexPath.row];
break;
case 1:
cell.textLabel.text =numberArray[indexPath.row];
break;
case 2:
cell.textLabel.text =alphaArray[indexPath.row];
break;
default:
break;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *titleButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
titleButton.frame = CGRectMake(0, 0, 320, 30);
[titleButton setBackgroundColor:[UIColor redColor]];
[titleButton addTarget:self action:@selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
titleButton.tag = section;
NSString *sectionName;
switch (section)
{
case 0: sectionName = NSLocalizedString(@"Colors", @"Colors");
break;
case 1: sectionName = NSLocalizedString(@"Numbers", @"Numbers");
break;
case 2: sectionName = NSLocalizedString(@"Alabaphet", @"Alabaphet");
break;
default: sectionName = @"";
break;
}
[titleButton setTitle:sectionName forState:UIControlStateNormal];
return titleButton;
}
-(void)handleTouchUpInside:(UIButton *)sender
{
if(selectedIndex== sender.tag)
{
selectedIndex=50;
}
else
{
selectedIndex= sender.tag;
}
[menuTableView reloadData];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// secondviewcontroller *sec =[[secondviewcontroller alloc]init];
switch (indexPath.section) {
case 0:
NSLog(@"%@",colorsArray[indexPath.row]);
//sec.arrayobjectInsecondClass =colorsArray[indexPath.row];
break;
case 1:
NSLog(@"%@",numberArray[indexPath.row]);
//sec.arrayobjectInsecondClass =numberArray[indexPath.row];
break;
case 2:
NSLog(@"%@",alphaArray[indexPath.row]);
//sec.arrayobjectInsecondClass =alphaArray[indexPath.row];
break;
default:
break;
}
// [self.navigationController pushViewController:sec animated:NO];
}