numberOfRowsInSection方法,包含可展开和可折叠的部分

时间:2014-01-21 17:15:51

标签: ios uitableview core-data

我正在尝试在tableView中实现可扩展和可折叠的部分。我正在按照教程去做,但我需要你的帮助。 expandedSectionsNSMutableIndexSetcanCollapseSectionBOOL method,声明如下:

-(BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section {

    if (section >0) return YES;

    return NO;

}

这是numberOfRowsInSection原始方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self tableView:tableView canCollapseSection:section])
    {
        if ([expandedSections containsIndex:section])
        {
            return 5; // return rows when expanded
        }

        return 1; // only top row showing
    }

    // Return the number of rows in the section.
    return 1;
}

这是我对同一方法的更新代码。我还实现了search barNSFetchedResultsController来填充节标题和行。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return [self.searchResults count];
    }
    else {



        if ([self tableView:tableView canCollapseSection:section])

        {
            if ([expandedSections containsIndex:section])
            {



            id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
                 return [sectionInfo numberOfObjects];
            }
            return 1;}



    }
    id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

应用程序没有崩溃,并显示了部分,但它没有按预期工作。也许你可以查看我的代码并告诉我那里有什么问题。 这是tableView的截图: enter image description here

1 个答案:

答案 0 :(得分:1)

(来自我的评论:)

第一部分与其他部分不同的原因是 您的canCollapseSection方法明确返回{0}的第{0}部分。

与获取的结果控制器结合可能存在另一个问题: 如果NO为折叠部分返回1(而不是该部分中的实际行数),那么您还必须修改FRC委托方法(这样他们就不会调用numberOfRowsInSection或{{ 1}}用于当前“不可见”的行。)

相关问题