Iphone 从uitableview中隐藏或删除节

Iphone 从uitableview中隐藏或删除节,iphone,uitableview,Iphone,Uitableview,我在应用程序中实现了一个分组表视图,用户可以从表的不同部分选择行 我想根据第一节行的选择隐藏(删除)特定节(包括标题) i、 e 第一部分的标题是“你们有车吗?”,答案是行选择中的是或否。 若用户选择“否”,则分组表中的第二节和第三节应隐藏(删除) 对于分组表中的收音机选择,我实现 我也参考了,但没有完全满足我的需要 请帮忙 编辑 TableView代表 myArray是我的数据源。 myArrayAnswerCount包含每个节的行数计数 -(NSInteger)numberOfSection

我在应用程序中实现了一个分组表视图,用户可以从表的不同部分选择行

我想根据第一节行的选择隐藏(删除)特定节(包括标题)

i、 e

第一部分的标题是“你们有车吗?”,答案是行选择中的是或否。 若用户选择“否”,则分组表中的第二节和第三节应隐藏(删除)

对于分组表中的收音机选择,我实现

我也参考了,但没有完全满足我的需要

请帮忙

编辑

TableView代表

myArray是我的数据源。 myArrayAnswerCount包含每个节的行数计数

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [myArray count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    for (int i = 0; i<[myArray count]; i++) {
        if (section==i) {
            return [[myArray objectAtIndex:i] title];
        }
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    for (int i = 0; i<[myArray count]; i++) {
        if (section==i) {
            return [[myArrayAnswerCount objectAtIndex:i] intValue];
        }
    }
}

您可以签入
UITableViewDataSource
如下:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView{
             if(YOUR_INSTANCE_BOOL_VAR==NO){
                   return 1;
             }else{
                   return 3;
             }
 }
用户选择“否”后,将您的“实例”布尔值设置为“否”,然后调用
[tableView reloadData]
; 显然,在用户选择“否”后,您还必须操纵
numberOfRowsInSection
,以触发所需的行数

另一种方法是调用:
-(void)deleteSections:(NSIndexSet*)带有RowAnimation的节:(UITableViewRowAnimation)animation

您可以通过以下方式获取Expath中的行:

+ (NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section; 
我解决了我的问题

[table beginUpdates];
[myArray removeObjectAtIndex:myIndexPath.section];
[table deleteSections:[NSIndexSet indexSetWithIndex:myIndexPath.section] withRowAnimation:UITableViewRowAnimationRight];
[table endUpdates];
“节0中的行数无效。更新(2)后现有节中包含的行数必须等于更新(3)前该节中包含的行数”错误中的问题是我忘记从myArrayAnswerCount数组中删除对象

所以最后,下面的代码

[table beginUpdates];
[myArray removeObjectAtIndex:myIndexPath.section];
[myArrayAnswerCount removeObjectAtIndex:myIndexPath.section]; //this is what i forgot.
[table deleteSections:[NSIndexSet indexSetWithIndex:myIndexPath.section] withRowAnimation:UITableViewRowAnimationRight];
[table endUpdates];

谢谢。

如果您不想删除节或行,只想隐藏它。您可以使用:

[tableView beginUpdates];
[tableView reloadSections:[NSindexSet indexSetWithIndex:(indexOfSection)] withRowAnimation:(UITableViewRowAnimationOfChoice)]; //hide section


如果您阅读文档,它会说这些方法的行为类似于删除/插入方法,但是它们实际上没有被删除,因此不需要删除/插入。您可以调用该方法来隐藏或取消隐藏。

@Mat:Hi,我必须选择使用-(void)deleteSections:(NSIndexSet*)带有行动画的节:(UITableViewRowAnimation)动画;方法,在成功从组中移除特定节后,如何更新在分组表中加载的数据(我是指NSMutableArray)?我不知道NSMutableArray中的对象类型和顺序,但使用NSMutableArray,您可以通过访问索引来添加/删除/替换任何对象。@Mat:hi,删除节0时,我遇到错误“节0中的行数无效。更新(2)后现有节中包含的行数必须等于更新(3)前该节中包含的行数”。删除节时,是否强制要求节1的行数必须与节0的行数相等?这是一个典型问题,可能是因为在修改数据源之前会调用reloadData,但是,如果您编辑您的问题并发布tableviewdelegate和tableviewdatasource回调,也许我们可以更好地理解问题出在哪里。@Mat:嗨,我解决了问题,实际上我忘了从myArrayAnswerCount中删除对象。这就是为什么它让我看到了这样的错误。感谢帮助。请看这里:myArrayAnswerCount是[myArray count]的值还是其他值。如果我给出[[myArray count]removeObjectAtIndex:],它会抛出一个警告“method removeObjectAtIndex”未找到,请帮助我,提前谢谢:)@Eshwar Chaitanya:myArrayAnswerCount包含表中每个节的行数。很明显,这是一个可变数组。我每个节只有一行,每次在“添加提醒”页面中添加提醒时,节数都会有所不同,请相应地建议我,提前感谢:)我的意思是,当我按“编辑”时,我可以按-符号并删除单元格,但是,当我返回并进入执行编辑和删除操作的视图时,删除的单元格仍然可用,如何使节永久删除,谢谢:)@EshwarChaitanya:myArrayAnswerCount只是一个可变数组,其中包含表中每个节的行数(这将是一个整数计数,在您的情况下,每个部分的值可能是1)。包括您还需要从数据源中删除值(您可能已经使用了可变数组)。首先检查该值。还需要相应地调试代码。
[tableView beginUpdates];
[tableView reloadSections:[NSindexSet indexSetWithIndex:(indexOfSection)] withRowAnimation:(UITableViewRowAnimationOfChoice)]; //hide section
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationOfChoice)]; //hide rows
[tableView endUpdates];