使用UITableView中的标题部分展开/折叠行

时间:2015-04-20 11:56:48

标签: ios uitableview

您好我已经实现了示例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;
    }
}

2 个答案:

答案 0 :(得分:0)

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

}

像上面一样修改此方法..

答案 1 :(得分:0)

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

}

Try this..

For Ur Code..Initially ur rows are 0. So, After u click event methods try to insert ur array no.of.rows.. Thats causing error.

相关问题