iphone - 在 UITableView 中展开部分,当展开多个部分时会崩溃

标签 iphone ios uitableview crash

尝试展开 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).'

最佳答案

阅读错误消息并检查 numberOfRowsInSectiondidSelectRowAtIndexPath 中的逻辑。说起来很简单:

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).'

很难比 iOS 已经解释得更清楚了。您可能正在重新加载表,然后表会抛出一个错误,指出第 0 部分(您的第一部分)中的行数在更新之间不同,并且这是不允许的。检查您的逻辑,以确定更新表之前和之后第 0 部分中有多少行。

[编辑]

didSelectRowAtIndexPath:中,如果helpOn == YES,则设置selectedCellIndexPath = indexPath.section。然后,您重新加载该部分的表,从而触发 numberOfRowsInSectio: 。在 numberOfRowsInSection: 中,如果 helpOn == YES 并且 section == selectedCellIndexPath 您返回 2。这可能就是为什么您在更新,更新后2。

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

[编辑2]

旁注:您的 cellForRowAtIndexPath 每次都会分配一个新单元格。这是低效的。您的 if(cell == nil) {//create new cell } 不需要 else。

关于iphone - 在 UITableView 中展开部分,当展开多个部分时会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17988301/

相关文章:

iphone - 如何使表格 View 单元格作为按钮工作?

iphone - 如何处理用户数据在不同时间到达的多个推送通知?

iphone - uitableview 细胞图像 - 寻找加号

ios - 通过异步调用问题将数据加载到数组中(Geofire、Swift)

iOS 将电话号码转换为国际格式

ios - 一个 Controller 中的两个 TableView

swift - 具有多个数组的 TableView segue

ios - 如何在tableview中显示大量数据

iphone - objc_msgSend() 错误消息,为什么?

ios - 在 Mac Pro 上使用 PhoneGap 的开发平台