Iphone 如何在单击第二个分区时关闭另一个展开的分区
我正在创建一个包含可扩展部分的应用程序。我已经用教程实现了这一点Iphone 如何在单击第二个分区时关闭另一个展开的分区,iphone,ios,uitableview,Iphone,Ios,Uitableview,我正在创建一个包含可扩展部分的应用程序。我已经用教程实现了这一点 但现在我想在单击另一个分区时关闭第一个扩展分区。有人能帮我吗。您可以将表视图:didSelectRowAtIndexPath:中的逻辑代码移动到一个新方法(switchSection)中,并在展开新节之前在展开的节上调用它以折叠它。请注意,您不再需要设置expandedSections,因为您只需要扩展一个分区 NSUInteger expandedSection; /// Define this instead of expan
但现在我想在单击另一个分区时关闭第一个扩展分区。有人能帮我吗。您可以将
表视图:didSelectRowAtIndexPath:
中的逻辑代码移动到一个新方法(switchSection
)中,并在展开新节之前在展开的节上调用它以折叠它。请注意,您不再需要设置expandedSections
,因为您只需要扩展一个分区
NSUInteger expandedSection; /// Define this instead of expandedSections
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self tableView:tableView canCollapseSection:indexPath.section]) {
if (!indexPath.row) {
NSInteger section = indexPath.section;
BOOL currentlyExpanded = (expandedSection == section);
/// Collapse expanded section
if (expandedSection >= 0) {
[self switchSection:expandedSection];
}
/// Expand the new section if needed
if (!currentlyExpanded) {
[self switchSection:section];
}
}
}
}
- (void)switchSection:(NSInteger)section {
BOOL currentlyExpanded = (expandedSection == section);
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if (currentlyExpanded) {
expandedSection = -1;
rows = [self tableView:tableView numberOfRowsInSection:section];
} else {
expandedSection = section;
rows = [self tableView:tableView numberOfRowsInSection:section];
}
for (int i = 1; i < rows; i++) {
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
[tmpArray addObject:tmpIndexPath];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (currentlyExpanded) {
[tableView deleteRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
} else {
[tableView insertRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
}
nsuiger expandedSection;//定义此部分,而不是展开的部分
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
if([self tableView:tableView canCollapseSection:indexPath.section]){
如果(!indexPath.row){
NSInteger section=indexPath.section;
BOOL currentlyExpanded=(expandedSection==section);
///塌陷扩展段
如果(expandedSection>=0){
[自切换段:扩展段];
}
///如果需要,展开新部分
如果(!currentlyExpanded){
[自切换段:段];
}
}
}
}
-(无效)道岔区段:(NSInteger)区段{
BOOL currentlyExpanded=(expandedSection==section);
NSInteger行;
NSMutableArray*tmpArray=[NSMutableArray];
如果(当前扩展){
expandedSection=-1;
行=[self tableView:tableView numberOfRowsInSection:section];
}否则{
expandedSection=截面;
行=[self tableView:tableView numberOfRowsInSection:section];
}
对于(int i=1;i
苹果提供了一个示例代码。这里是链接
但是其中有一个小错误,请参考。只要在展开节时从展开节列表中删除所有内容……也许可以使用int来存储打开节的值。单击节时,您可以更改此值,并在方法
tableView:numberofrowsinssection:
中返回0或您的行数,具体取决于节数是否等于存储的节数thanx作为答案,如` for `中所示(展开节中的NSInteger项)`此代码expandedSections是NSMutableIndexSet
,因此我无法检查项目是否在该行中,因为它给了我错误检查更新的答案。您必须定义nsuiger expandedSection
,而不是索引集。