【问题标题】:Expand/collapse rows using header section in a UITableView使用 UITableView 中的标题部分展开/折叠行
【发布时间】:2015-06-27 03:21:31
【问题描述】:

您好,我已经实现了示例Expand Table

-(void)sectionButtonTouchUpInside:(UIButton*)sender
{
//    sender.backgroundColor = [UIColor greenColor];
    [self.tableView beginUpdates];
    int section = sender.tag;
    
    bool shouldCollapse = ![collapsedSections containsObject:@(section)];
    if (shouldCollapse)
    {
        int numOfRows = [self.tableView numberOfRowsInSection:section];
        NSArray* indexPaths = [self indexPathsForSection:section withNumberOfRows:numOfRows];
        [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
        [collapsedSections addObject:@(section)];
    }
    else
    {
        int numOfRows = 0 ;
        switch (section)
        {
            case 0:
                numOfRows = (int)aArray.count;
                break;
            case 1:
                numOfRows = (int)bArray.count;
                break;
            case 2:
                numOfRows = (int)cArray.count;
                break;
            case 3:
                numOfRows = (int)dArray.count;
                break;
        }
        
        NSArray* indexPaths = [self indexPathsForSection:section withNumberOfRows:numOfRows];
        [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
        [collapsedSections removeObject:@(section)];
    }
    [self.tableView endUpdates];
}

我的程序在上面的代码中崩溃并显示此消息:

由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'无效更新:无效 第 2 节中的行数。 更新后的现有节(1)必须等于 更新 (1) 之前该部分中包含的行,加号或减号 从该部分插入或删除的行数(0 插入, 1 已删除)并加上或减去移入或移出的行数 该部分(0 移入,0 移出)。

更新

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(![_collapsedSections containsObject:@(section)])
    {
        return 0;
    }
    else
    {
        int count = 0;
        switch (section)
        {
            case 0:
                count = (int)aArray.count;
                break;
            case 1:
                count = (int)bArray.count;
                break;
            case 2:
                count = (int)cArray.count;
                break;
            case 3:
                count = (int)dArray.count;
                break;
        }
        
        return count;
    }
}

【问题讨论】:

  • 我不知道这个程序将如何从给定的数组中删除行以及如何再次填充以显示它们。
  • 移动 [collapsedSections addObject:@(section)]; & [collapsedSections removeObject:@(section)];在从表中插入或删除行之前。
  • 您面临的错误是由于您的模型没有更新。这里的模型是你的数组。每当您想删除一行时,您需要先从数组中删除该特定对象,然后调用 tableView 删除函数。由于任何画面更新都会调用 numberOfSections、numberOfRows 和 cellForRowAtIndexPath 方法。
  • 根据你的方法更新cl.ly/ajsi

标签: ios uitableview


【解决方案1】:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    int count = 0;
    switch (section)
    {
        case 0:
            count = (int)aArray.count;
            break;
        case 1:
            count = (int)bArray.count;
            break;
        case 2:
            count = (int)cArray.count;
            break;
        case 3:
            count = (int)dArray.count;
            break;
    }

    return count;

}

试试这个..

对于你的代码..最初你的行是 0。所以,在你点击事件方法后尝试插入你的数组 no.of.rows..这会导致错误。

【讨论】:

  • 感谢您的回复。 . .问题由 Mr.Abid Hussain 解决。
【解决方案2】:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if(section == 0)
{
  return [__collapse containsObject:@(section)] ? 0 : [self.section1 count];
}
else if(section == 1)
{
   return [__collapse containsObject:@(section)] ? 0 : [self.section2 count];

}
else if(section == 2)
{
     return [__collapse containsObject:@(section)] ? 0 : [self.section3 count];
}
else
{
    return 0;
}

}

像上面一样修改这个方法..

【讨论】:

  • Ok Mr.Jr Developer :) 感谢您的回复。我已经实现了上面更新中提到的协议方法,但是发现上面的错误。
猜你喜欢
  • 2013-06-19
  • 1970-01-01
  • 2010-12-28
  • 1970-01-01
  • 1970-01-01
  • 2013-03-23
  • 1970-01-01
  • 2019-08-31
  • 2014-10-01
相关资源
最近更新 更多