简体   繁体   English

隐藏UITableView部分以及其中的所有行

[英]Hiding a UITableView section along with all the rows in it

I have 2 sections on my UITableview controller. 我的UITableview控制器上有2个部分。 One of my section has a switch and my requirement is when I set the switch to ON, the second section should be set to hidden along with all its rows. 我的一部分中有一个开关,我的要求是,当我将开关设置为ON时,第二部分及其所有行应设置为隐藏。 I am calling the following method/code to hide the section when the status of the switch is changed: 当开关的状态更改时,我正在调用以下方法/代码来隐藏该部分:

- (void)setState
{
    myTableViewCell *myCell = [[myTableViewCell alloc] init];
    if ([myCell.mySwitch isOn])
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:1];
        [self.tableView cellForRowAtIndexPath:indexPath].hidden = YES;
    }
}

I am getting the following exception for this code which I understand is perfectly true. 我收到此代码的以下异常,据我所知是完全正确的。

Name = NSInternalInconsistencyException;
Reason = "Invalid index path for use with UITableView.  Index paths passed to table view must contain exactly two indices specifying the section and row.  Please use the category on NSIndexPath in UITableView.h if possible.";

But how can I hide the complete section along with all its rows. 但是,如何隐藏完整的部分及其所有行。 If I try to get the index path using NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1]; 如果我尝试使用NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];获取索引路径NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1]; this would just hide the 1st row in the section. 这只会隐藏该部分的第一行。

If you wanna hide a whole section when switch is on, just reloadData when you click the switch, and return 0 in numberOfRowsInSection for that section and return totalNumberOfSections - howManySwitchesIsOn in numberOfSectionsInTableView , like this: 如果你想隐藏一整节,当开关接通时,只是reloadData当你点击开关,并在返回0 numberOfRowsInSection该节和返回totalNumberOfSections - howManySwitchesIsOnnumberOfSectionsInTableView ,就像这样:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView   
{  
    if(howManySwitchesIsOn) {
        return totalNumberOfSections - howManySwitchesIsOn; 
    } 
    return totalNumberOfSections;
}  

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
{  
    if(section should be hide) {
         return 0;
    }
    return howManyRowsForThatSection;
} 

Section deletion and insertion in a grouped UITableView are accomplished via: 节的删除和在分组的UITableView中的插入是通过以下方式完成的:

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation

Check out SectionHidingDemo , a demo app that illustrates section deletion and insertion in a grouped UITableView using those methods. 请查看SectionHidingDemo ,这是一个演示应用程序,它演示了使用这些方法在节段的UITableView删除和插入节段。

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

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