首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >展开部分UITableview when when reloadSections

展开部分UITableview when when reloadSections
EN

Stack Overflow用户
提问于 2013-07-31 21:38:30
回答 2查看 2.4K关注 0票数 1

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

代码语言:javascript
复制
-(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).'

EN

回答 2

Stack Overflow用户

发布于 2013-07-31 21:56:19

当您更改部分中的单元格数量时,需要使用beginUpdatesendUpdates包装重载操作,并使用insertRowsAtIndexPaths:withRowAnimationdeleteRowsAtIndexPaths:withRowAnimation:指定单元格的插入和删除。

或者你可以使用'reloadData`来重新加载所有的单元格,但是如果你使用它,无论是动画还是用户体验都会有问题。

票数 2
EN

Stack Overflow用户

发布于 2013-07-31 21:50:00

将其从您的代码中删除。

代码语言:javascript
复制
else{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

如果单元格为nil,则不需要分配单元格,tableview将重用该行中的前一个单元格

代码语言:javascript
复制
cell = [self.mHelpTable dequeueReusableCellWithIdentifier:cellIdentifier];  

在.h中制造

代码语言:javascript
复制
int selectedCellIndexPath;
BOOL selected;

在.m中

代码语言:javascript
复制
-(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];

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17971991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档