【问题标题】:UITableViewCell Expandable CrashUITableViewCell 可扩展崩溃
【发布时间】:2013-03-20 23:38:14
【问题描述】:

我遇到了关于可扩展 tableviewcell 的问题。当我尝试展开 tableviewcell 时它崩溃了。 我不明白应用程序在哪里崩溃。请建议我一些解决方案。 请帮帮我。

我的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   if (appDelegate.array_proj == (id)[NSNull null])
        return 0;
    else
        return [appDelegate.array_proj count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if ([expandedSections containsIndex:section])
    {
        if ([appDelegate.array_task count]==0)
        {
            return 0;
        }
        else
        {
            NSLog(@"array task count: %d",[appDelegate.array_task count]);
            return [appDelegate.array_task count];
        }
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell ==nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor colorWithRed:53.0/255 green:53.0/255 blue:53.0/255 alpha:1.0];

    UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:CGRectZero];
    cell.backgroundView = backgroundView;
    backgroundView.image = [UIImage imageNamed:@"prjctcell_bg.png"];

    if (!indexPath.row)
    {
        objePro = [appDelegate.array_proj objectAtIndex:indexPath.section];

        cell.textLabel.text = objePro.projctname;
        appDelegate.intForPid=objePro.pojctid;

        if ([expandedSections containsIndex:indexPath.section])
        {
            cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor blackColor] type:DTCustomColoredAccessoryTypeUp];
        }
        else
        {
            cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor blackColor] type:DTCustomColoredAccessoryTypeDown];
        }
    }
    else
    {
        if (appDelegate.array_task != (id)[NSNull null])
        {
            objePro = [appDelegate.array_proj objectAtIndex:appDelegate.storeAppDelegateIndex];
            objeTask = [appDelegate.array_task objectAtIndex:indexPath.section];

            cell.textLabel.text = objeTask.taskname;
            cell.backgroundView = nil;
            cell.accessoryView = nil;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
{
    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    objePro = [appDelegate.array_proj objectAtIndex:indexPath.section];
    appDelegate.intForPid=objePro.pojctid;

   [appDelegate selectTask:appDelegate.intForPid];

    if (!indexPath.row)
    {
        [tblView beginUpdates];

        //only first row toggles expand/collapse
        [tblView deselectRowAtIndexPath:indexPath animated:YES];

        NSInteger sections = indexPath.section;
        BOOL currentlyExpanded = [expandedSections containsIndex:sections];
        NSInteger rows;

        NSMutableArray *tmpArray = [NSMutableArray array];
        if (currentlyExpanded)
        {
            rows = [self tableView:tblView numberOfRowsInSection:sections];
            [expandedSections removeIndex:sections];
        }
        else
        {
            [expandedSections addIndex:sections];
            rows = [self tableView:tblView numberOfRowsInSection:sections];
        }
        for (int i=1; i<rows; i++)
        {
            NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:sections];
            [tmpArray addObject:tmpIndexPath];
        }
        UITableViewCell *cell = [tblView cellForRowAtIndexPath:indexPath];

        if (currentlyExpanded)
        {

            [tblView deleteRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];
            cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor blackColor] type:DTCustomColoredAccessoryTypeDown];
        }
        else
        {
            [tblView insertRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];
            cell.accessoryView =  [DTCustomColoredAccessory accessoryWithColor:[UIColor blackColor] type:DTCustomColoredAccessoryTypeUp];
        }
        [tblView endUpdates];
    }
}

它给出了错误: -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1070 中的断言失败 2013-03-20 19:14:00.102 Daily Achiever[2886:c07] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 1 节中的行数无效。更新后现有节中包含的行数 (3) 必须等于更新前该节中包含的行数 (1),加上或减去从该节中插入或删除的行数(0 插入, 0 已删除)并加上或减去移入或移出该部分的行数(0 移入,0 移出)。'

【问题讨论】:

    标签: iphone uitableview ios6


    【解决方案1】:

    您的问题在于以下代码行:

     [tblView deleteRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];
    

     [tblView insertRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];
    

    您发布的崩溃是由于第一个崩溃造成的。在这些行中,您尝试从表中插入/删除行,但您的 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 方法没有相应更改。

    每当您从表中添加/删除单元格时,您似乎都想从 appDelegate.array_task 数组中添加/删除项目(因为这是您确定行数的数组)。

    作为旁注,以下行:

     if ([appDelegate.array_task count]==0) { 
        return 0;
     }
    

    什么都不做,因为你有else return [appDelegate.array_task count],这意味着如果[appDelegate.array_task count]==0无论如何都会返回0。

    【讨论】:

    • 你能给我解决办法吗..?我从前两天尝试了很多东西,但仍然遇到问题
    • 要么在您“解展开”表格时从您的appDelegate.array_task 中删除一个对象,并在您“展开”表格时添加一个对象,或者更改您的- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 以考虑是否表当前是否展开(即有一个变量,如果展开则加一,否则加0)。
    • 我刚刚解决了这个问题,但是当我点击部分和单元格展开时,它显示的不是第 1 行,而是第 2 行。 :(
    • 再一次,我唯一能想到的可能是您的数据中正在填充表格的东西。在插入或删除之后形成的新表将与最初使用添加/删除这些单元格创建的表完全相同。
    【解决方案2】:

    添加部分时使用此代码

    NSArray *insertIndexPaths = [NSArray arrayWithObjects:
                                 [NSIndexPath indexPathForRow:2 inSection:0],
                                 [NSIndexPath indexPathForRow:3 inSection:0],
                                 nil];
    

    用于插入行

    [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
    

    如果你要添加两个部分

     [tableView insertSections:[NSIndexSet indexSetWithIndex:indexPath.section + 1] withRowAnimation:UITableViewRowAnimationAutomatic];
       [tableView insertSections:[NSIndexSet indexSetWithIndex:indexPath.section + 2] withRowAnimation:UITableViewRowAnimationAutomatic];
    

    同样的方式删除部分

    [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:YES];
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section - 1] withRowAnimation:YES];
    

    行删除

     [tableView deleteRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多