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

时间:2013-12-04 23:40:14

标签: ios uitableview uiswitch

我的UITableview控制器上有2个部分。我的一个部分有一个开关,我的要求是当我将开关设置为ON时,第二部分应设置为隐藏及其所有行。当调整开关状态时,我调用以下方法/代码隐藏该部分:

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

我对此代码有以下异常,我理解这是完全正确的。

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.";

但是如何隐藏整个部分及其所有行。如果我尝试使用NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];获取索引路径,则只会隐藏该部分中的第一行。

2 个答案:

答案 0 :(得分:0)

如果你想在开关打开时隐藏整个部分,当你点击开关时只需reloadData,然后在numberOfRowsInSection中为该部分返回0并在{{1}中返回totalNumberOfSections - howManySwitchesIsOn像这样:

numberOfSectionsInTableView

答案 1 :(得分:0)

分组UITableView中的部分删除和插入可通过以下方式完成:

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

查看SectionHidingDemo,这是一个演示应用,用于说明使用这些方法删除和插入分组UITableView中的部分。

相关问题