简体   繁体   English

如何以编程方式关闭展开的表格视图单元格?

[英]How to close expanded table view cell programmatically?

I have two tabs in a view controller: on click of each tab, different data is displayed in a table view with cells that expand and close on click. 我在视图控制器中有两个选项卡:单击每个选项卡时,表视图中会显示不同的数据,并带有单击时会展开和关闭的单元格。

The problem is that when I'm in tab A, and expand a cell there, then go and click on tab B, loading new data, that cell is still expanded. 问题是,当我进入选项卡A并在其中展开一个单元格时,然后单击选项卡B并加载新数据,该单元格仍在扩展。 How do I close it programatically? 如何以编程方式关闭它?

I'm using the FZAccordionTableView subclass to perform the expansions. 我正在使用FZAccordionTableView子类来执行扩展。

- (UIView *)tableView:(FZAccordionTableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kAccordionHeaderViewReuseIdentifier];
    if (listOfCategoryWhichHaveItems.count > 0) {
        if (arrCategory.count > 0) {
            arrCategory1  = _btnProduct.selected == YES ? [arrCategory objectAtIndex:0] : [arrCategory objectAtIndex:1];
            NSDictionary *dict = arrCategory1[section];
            view.lblHeaderTitel.text = [dict valueForKey:kCategory];
            bool isSelected = [tableView isSectionOpen:section];
        }
    }
    return view;
}

#pragma mark - <FZAccordionTableViewDelegate> -

- (void)tableView:(FZAccordionTableView *)tableView willOpenSection:(NSInteger)section withHeader:(UITableViewHeaderFooterView *)header {
    loadFirstTime = false;
    [tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation: UITableViewRowAnimationAutomatic];
    if (listOfCategoryWhichHaveItems.count > 0) {
        _sectionNumber = &section;
        NSDictionary *dict = listOfCategoryWhichHaveItems[section];
        [self setDataAccordingToCategories:[dict valueForKey:kCategory]];
    }
}

- (void)tableView:(FZAccordionTableView *)tableView didOpenSection:(NSInteger)section withHeader:(UITableViewHeaderFooterView *)header {
}

单击选项卡B时,请将表的keepOneSectionOpen属性设置为No。

Yourtable.keepOneSectionOpen = No;

In the FZAccordionTableView GitHub repo i found this method: FZAccordionTableView GitHub存储库中,我找到了此方法:

- (void)closeSection:(NSInteger)section withHeaderView:(nullable FZAccordionTableViewHeaderView *)sectionHeaderView

That will probably do what you want, it is however not declared in the header file so you can't call it directly. 那可能会做您想做的事,但是它没有在头文件中声明,因此您不能直接调用它。

You can however clone the repo, add the line to the header file, add + commit + push to your fork. 但是,您可以克隆存储库,将行添加到头文件中,添加+ commit + push到fork中。 Then use that repo in your Podfile (or however you're using the library right now) so you're using your fork of the framework, then you can call the method. 然后使用该回购在Podfile(或无论你正在使用的库现在)让你在使用你的框架叉,那么你可以调用该方法。

On click of each tab, i used this [_tblProductServies closeAllSectionsExcept:-1]; 单击每个选项卡时,我使用了此[_tblProductServies closeAllSectionsExcept:-1]; its working fine now, thanks for all. 现在一切正常,谢谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM