2013-08-01 119 views
0

尝试展开UITableView中的部分,如果单个部分展开并关闭,那么它的确定,但如果一个部分展开,那么另一个不关闭先前它会崩溃。 以下是我正在尝试的代码。展开UITableView中的部分时,展开多个部分时会崩溃

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
if(!helpOn) 
    return 1; 
else 
    if(section == selectedCellIndexPath) 
    { 
    return 2; 
    } 
    else{ 
     return 1; 
    } 
return 1; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *cellIdentifier = @"CellIdentifier"; 
UITableViewCell *cell; 
cell = [self.mHelpTable dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 
else{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 

UILabel *txtQues = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 310, 30)]; 
txtQues.backgroundColor = [UIColor clearColor]; 
txtQues.lineBreakMode = NSLineBreakByWordWrapping; 
txtQues.numberOfLines = 2; 
txtQues.userInteractionEnabled = FALSE; 

UITextView *txtAns = [[UITextView alloc]initWithFrame:CGRectMake(5, 10, 310, 60)]; 
txtAns.backgroundColor = [UIColor clearColor]; 
txtAns.userInteractionEnabled = FALSE; 

txtQues.font = [UIFont fontWithName:@"Helvetica-Bold" size:13.0]; 

if(!helpOn) 
//if (indexPath.section==selectedCellIndexPath) 
{ 
    if(indexPath.row == 0) 
     [cell.contentView addSubview:txtQues]; 
     txtQues.text = [self.mArrQues objectAtIndex:indexPath.section]; 
} 
else 
{ 
    if(indexPath.row == 0) 
    { 
     [cell.contentView addSubview:txtQues]; 
     txtQues.text = [self.mArrQues objectAtIndex:indexPath.section]; 
    } 
    else{ 
     [cell.contentView addSubview:txtAns]; 
     txtAns.text = [self.mArrAns objectAtIndex:indexPath.section]; 
    } 
} 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 

return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
helpOn = !helpOn; 

int ind = indexPath.section; 
if(ind == selectedCellIndexPath) 
{ 
} 
else{ 
    helpOn = YES; 
} 
if(helpOn) 
{ 
    selectedCellIndexPath = indexPath.section; 
[self.mHelpTable reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade]; 
} 
else 
{ 
    if(indexPath.row == 0) 
    { 
    //selectedCellIndexPath = indexPath.section; 
    [self.mHelpTable reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 
} 

请指导以上我没有得到什么我们错了在这里已经度过了一个晚上和一个早晨。它在段方法中以行数崩溃。 以下是错误获取。

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' 

回答

1

阅读错误信息,并检查您的逻辑numberOfRowsInSectiondidSelectRowAtIndexPath。它说简单:

无效更新:在部分0的行的无效数包含在现有段的行的更新后的数字(2)必须等于前的包含在该部分的行数更新(1),加上或减去从该部分插入或删除的行数(0插入,0删除)以及正数或负数移入或移出该部分的行数(移入0,移出0)。 “

很难说明这一点,它已经比iOS已经清楚。您可能正在重新加载表,然后该表会引发错误,指出第0部分(您的第一部分)中的行数在更新之间不同,并且不允许。在更新表格之前和之后,检查确定第0节中有多少行的逻辑。

[编辑]

它看起来像在didSelectRowAtIndexPath:,如果你helpOn == YES设置selectedCellIndexPath = indexPath.section。然后你重新加载该部分的表,并因此numberOfRowsInSectio:火灾。在numberOfRowsInSection:如果helpOn == YESsection == selectedCellIndexPath您返回2.这可能是为什么你看到一个更新之前,和2更新后。

同样,我的建议是检查这两种方法的逻辑。更新之后,您正在更改其中一个部分的行。

[编辑2]

旁注:你cellForRowAtIndexPath每一次都被分配一个新的细胞。这是低效的。您的if(cell == nil) { // create new cell }不需要别的。

+0

我没有得到更新之前有一个行,并在第2部分更新两个之后。 –

+0

检查我的aedit,似乎你在'numberOfRowsInSection'的逻辑返回2.我会设置一个断点,并看到如果该if/else被正确输入 – Aaron

相关问题