Keeping one section open at a time in UITableView

2020-07-29 01:59发布

问题:

I'm building an app with a UITableView-based accordion view. This code is based on Apple sample code which can be found here or here. In the sample code, the problem I'm trying to fix is there, so I know that it's not something that I introduced.

If you open multiple sections without closing the previous one explicitly, multiple sections can be open at the same time. This can lead to a crash, which can be replicated by hitting buttons 1, 2, 3, 1, 1, 3.

In my app, I am trying to make the previous section close and the button in the header go to an unselected state when a new section is opened, so that you can only ever have one section opened and one section header button selected at a time. If anyone has any experience with this sample code or this use of a tableview, I'd love to correct this, especially since it's a problem inherent in Apple's own code

回答1:

The property APLSectionHeaderView* headerView of APLSectionInfo is never set. So set sectionInfo.headerView = sectionHeaderView in tableView delegate method.

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {

    APLSectionHeaderView *sectionHeaderView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];

    APLSectionInfo *sectionInfo = (self.sectionInfoArray)[section];

    sectionHeaderView.titleLabel.text = sectionInfo.play.name;
    sectionHeaderView.section = section;
    sectionHeaderView.delegate = self;
    sectionInfo.headerView = sectionHeaderView;
    return sectionHeaderView;
}