Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 展开和折叠UITableview_Iphone_Ios_Ipad_Uitableview - Fatal编程技术网

Iphone 展开和折叠UITableview

Iphone 展开和折叠UITableview,iphone,ios,ipad,uitableview,Iphone,Ios,Ipad,Uitableview,我正在使用下面的代码进行uitableview的展开和折叠。它可以正常工作。但是,当我选择任何部分时,它会展开但不会折叠以前展开的部分 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([self tableView:tableView canCollapseSection:indexPath.section]) { if (!inde

我正在使用下面的代码进行uitableview的展开和折叠。它可以正常工作。但是,当我选择任何部分时,它会展开但不会折叠以前展开的部分

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:tableView canCollapseSection:indexPath.section])
    {
    if (!indexPath.row)
    {
        // only first row toggles exapand/collapse
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
         NSInteger section = indexPath.section;
         BOOL currentlyExpanded = [expandedSections containsIndex:section];
        NSInteger rows;
        NSMutableArray *tmpArray = [NSMutableArray array];
    if (currentlyExpanded)
        {
            rows = [self tableView:tableView numberOfRowsInSection:section];
            NSLog(@"expanded");
            [expandedSections removeIndex:section];
            screenTypeReloadCheck = NO;
        }
        else
        {
            screenTypeReloadCheck =YES;
            [expandedSections addIndex: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];

            UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UITableExpand"]];
            cell.accessoryView = imView;
        }
        else
        {
            [tableView insertRowsAtIndexPaths:tmpArray
                             withRowAnimation:UITableViewRowAnimationTop];

            UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UITableContract"]];
            cell.accessoryView = imView;
        }
    }
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath
{
if([self tableView:tableView canCollapseSection:indexPath.section])
{
如果(!indexPath.row)
{
//仅第一行切换展开/折叠
[tableView取消行索引路径:indexPath动画:是];
NSInteger section=indexPath.section;
BOOL currentlyExpanded=[expandedSections包含索引:section];
NSInteger行;
NSMutableArray*tmpArray=[NSMutableArray];
如果(当前扩展)
{
行=[self tableView:tableView numberOfRowsInSection:section];
NSLog(@“扩展”);
[expandedSections removeIndex:section];
screenTypeReloadCheck=否;
}
其他的
{
screenTypeReloadCheck=是;
[扩展节添加索引:节];
行=[self tableView:tableView numberOfRowsInSection:section];
}

对于(int i=1;iTableView部分不是折叠而是展开。因为,您的currentlyExpanded总是返回NO。所以,您总是得到展开

if (currentlyExpanded)
        {
   rows = [self tableView:tableView numberOfRowsInSection:section];
            NSLog(@"expanded");
            [expandedSections removeIndex:section];
            screenTypeReloadCheck = NO;
        }
        else
        {
            screenTypeReloadCheck =YES;
            [expandedSections addIndex:section];
            rows = [self tableView:tableView numberOfRowsInSection:section];
        }

我不知道如何设置currentlyExpanded值。如果您需要更多帮助,请再分享几行代码。

我在我的应用程序中也做了同样的事情。下面是解决方案

在您的.h中

#import "sampleCustomCell.h"

@interface second : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
   NSMutableDictionary *selectedIndexes;
   sampleCustomCell *samplCustom;
   UITableView *tblTempData;
   NSMutableArray *yourAry;
}

@property(nonatomic,retain) sampleCustomCell *samplCustom;
@property (nonatomic,retain) NSMutableArray *yourAry;
@property (nonatomic,retain) IBOutlet UITableView *tblTempData;

@end

希望这有助于..愉快地编码..谢谢..

:-请参见以下链接中的答案:-。如果(!expandedSections){expandedSections=[[NSMutableIndexSet alloc]init];},这将有助于Hi@peter的可能重复
@synthesize samplCustom;
@synthesize tblTempData;

BOOL isSelected;

int kCellHieght=0;

- (void)viewDidLoad
{
   [super viewDidLoad];
   yourAry = [[NSMutableArray alloc] initWithObjects:@"1_id",@"2_id",@"3_id",@"4_id",@"5_id",@"6_id",@"7_id",@"8_id",@"9_id",@"10_id", nil];
   selectedIndexes = [[NSMutableDictionary alloc] init];
}


#pragma mark TableView with Expand and collapse


- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
    // Return whether the cell at the specified index path is selected or not
    NSLog(@"%@", selectedIndexes);
    NSNumber *selectedIndex = [selectedIndexes objectForKey:indexPath];
    NSLog(@"%@", selectedIndex);
    return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
 }


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([self cellIsSelected:indexPath])
    {
       kCellHieght = 200; //Expanded height for your customCell xib.
    }
    else
    {
       kCellHieght = 130; // Height of your customCell xib
    }
    return kCellHieght;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [yourAry count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    sampleCustomCell *cell;
    if (tableView.tag == 11)
    {
       cell = (sampleCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"sampleCustomCell"];
       NSArray *nib;
       for (UIControl *subview in cell.contentView.subviews) {
          [subview removeFromSuperview];
       }
       if(cell == nil)
       {
           nib = [[NSBundle mainBundle] loadNibNamed:@"sampleCustomCell" owner:self options:nil];

           for(id oneobject in nib)
           {
              if([oneobject isKindOfClass:[sampleCustomCell class]])
              {
                  cell = (sampleCustomCell *)oneobject;
                  //[cell setIndexpath:indexPath];
                  //[cell setDelegete:self];
                  cell.lblProduct.text = [yourAry objectAtIndex:[indexPath row]];
              }
          }
       }

       if([self cellIsSelected:indexPath])
       {
          [UIView beginAnimations:nil context:nil];
          [UIView setAnimationDuration:3];
          [UIView commitAnimations];
         [self performSelector:@selector(showHidden:) withObject:cell afterDelay:0.4];
       }
    }
    return cell;
}


- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath
{

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    isSelected = ![self cellIsSelected:indexPath];
    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];
    BOOL isSelected = ![self cellIsSelected:indexPath];
    NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
    [selectedIndexes removeAllObjects];
    [self hideTV:tableView];
    [selectedIndexes setObject:selectedIndex forKey:indexPath];
    NSLog(@" array %@", selectedIndexes);
    [self.tblTempData beginUpdates];


    sampleCustomCell *selectedCell = (sampleCustomCell *)[tblTempData cellForRowAtIndexPath:indexPath];
    if([self cellIsSelected:indexPath])
    {
        samplCustom = selectedCell;
        [UIView animateWithDuration:15 delay:0.2 options:UIViewAnimationCurveLinear animations:^{NSLog(@"animating");} completion:^(BOOL finished){
            NSLog(@"Done 1!");
        }];
        [UIView commitAnimations];
    }
    else
    {
        [UIView animateWithDuration:5 delay:0.2 options:UIViewAnimationCurveLinear animations:^{NSLog(@"animating");} completion:^(BOOL finished){
            NSLog(@"Done 1!");
        }];
        [UIView commitAnimations];
    }
    [self.tblTempData endUpdates];
    [tblTempData reloadData];
}

-(void)hideTV:(UIView *) view{

    [tblTempData reloadData];

    for (id View in [view subviews]) {
       if ([View isKindOfClass:[UITextView class]]) {
           [View setHidden:YES];
       }

       if ([View isKindOfClass:[UITableViewCell class]]) {
           UITableViewCell *cell = (UITableViewCell *) View;
           [self hideTV:cell.contentView];
       }  
   }
}


-(void) showHidden : (UITableViewCell *)cell{
    for (id View in [cell subviews]) {
        if ([View isKindOfClass:[UITextView class]]) {
            if ([View tag] == 111) {
               [View setHidden:NO];
           }
       } 
   }
}