【问题标题】:Expanding sections in UITableView, when expanding multi sections gets crash在 UITableView 中展开部分,当展开多个部分时会崩溃
【发布时间】:2013-08-01 08:00:50
【问题描述】:

尝试在 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];
    }
}
}

请指导以上我不明白我们在这里出了什么问题已经花了一个晚上和一个早上。它在 section 方法中的行数处崩溃。 以下是错误获取。

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

【问题讨论】:

    标签: iphone ios uitableview crash


    【解决方案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 } 不需要 else。

    【讨论】:

    • 我没明白更新前有一行,更新后第 0 节有两行。
    • 检查我的 aedit,您在 numberOfRowsInSection 中的逻辑似乎返回 2。我会设置一个断点,看看 if/else 是否输入正确
    猜你喜欢
    • 2019-08-31
    • 1970-01-01
    • 2010-12-28
    • 2019-03-15
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    相关资源
    最近更新 更多