繁体   English   中英

展开部分UITableview在reloadSections时崩溃

[英]Expand section UITableview getting crash when reloadSections

尝试扩展部分时,每个部分在展开时将有两行。 它正在崩溃。 以下是我正在尝试的代码。

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.mArrQues count];
}

-(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];
}

UIView* myBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
myBackgroundView.backgroundColor = [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0];
cell.backgroundView = myBackgroundView;
//[cell addSubview:myBackgroundView];

if(!helpOn)
{
    cell.textLabel.text = [self.mArrQues objectAtIndex:indexPath.section];
}
else
{
if(indexPath.row == 0)
    {
    cell.textLabel.text = [self.mArrQues objectAtIndex:indexPath.section];
    }
else{
    cell.textLabel.text = [self.mArrAns objectAtIndex:indexPath.section];
    }
}

cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;
}

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

int ind = indexPath.section;
if(ind == selectedCellIndexPath)
{
}
else{
    helpOn = 1;
}
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).'

从代码中删除它。

else{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

如果它是nil,则不需要分配您的单元格,tableview将从此行中的前一个单元格重用

cell = [self.mHelpTable dequeueReusableCellWithIdentifier:cellIdentifier];  

制作.h

int selectedCellIndexPath;
BOOL selected;

在.m

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{    
    if (section==selectedCellIndexPath)
    {
        //selected > this is because initialy selectedCellIndexPath is 0 and section is also 0
        if (selected) {
        return 2;
        }
        else{
            return 1;
        }
    }
    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];
    }

    UIView* myBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
    myBackgroundView.backgroundColor = [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0];
    cell.backgroundView = myBackgroundView;
    //[cell addSubview:myBackgroundView];

    //if(!helpOn)
    if (indexPath.section!=selectedCellIndexPath)
    {
        cell.textLabel.text = [mArrQues objectAtIndex:indexPath.section];
    }
    else
    {
        if(indexPath.row == 0)
        {
            cell.textLabel.text = [mArrQues objectAtIndex:indexPath.section];
        }
        else{
            cell.textLabel.text = [mArrAns objectAtIndex:indexPath.section];
        }
    }

    //cell.textLabel.text = [self.mArrQues objectAtIndex:indexPath.section];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selected= !selected;
    selectedCellIndexPath = indexPath.section;
    //selectedCellIndexPath = indexPath.row;
    [self.mHelpTable reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];

}

在更改section的单元格数时,需要使用beginUpdatesendUpdates包装重载操作,并使用insertRowsAtIndexPaths:withRowAnimation指定单元格插入和删除insertRowsAtIndexPaths:withRowAnimationdeleteRowsAtIndexPaths:withRowAnimation: .

或者,您可以使用'reloadData`重新加载所有单元格,但是如果您使用动画或用户体验,则会出现问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM