如何在导航栏按钮单击操作上展开和折叠UITableview单元格部分

时间:2016-01-13 05:27:48

标签: ios objective-c uitableview

请查看代码:

    ViewControlller.h

    @property (nonatomic, strong) IBOutlet UITableView *tablevw;
    @property (nonatomic , strong) IBOutlet UIButton *minusBtn;
    -(IBAction)minusBtnAction:(id)sender;

    ViewController.m
    @interface ViewController ()
    {
        NSArray *sectionTitleArray;
        NSMutableArray *arrayForBool;
        NSArray *applesArray;
        NSArray *grapesArray;
        UIButton *button;
        int clickedIndex;
    }
    - (void)viewDidLoad
    {
        [super viewDidLoad];

        _tablevw.delegate = self;
        _tablevw.dataSource =self;
        _tablevw.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tablevw.separatorStyle = UITableViewCellSeparatorStyleNone;


        [UIView setAnimationsEnabled:NO];


        arrayForBool = [[NSMutableArray alloc]init];

        clickedIndex = 0;


        sectionTitleArray = @[ @{@"text": @"Apples"},
                               @{@"text": @"Grapes"},
                               ];


              applesArray = @[ @{@"text1": @"Apple1"},
                               @{@"text1": @"Apple2"},
                               @{@"text1": @"Apple3"},
                               @{@"text1": @"Apple4"},
                               @{@"text1": @"Apple5"},
                               @{@"text1": @"Apple6"},
                               @{@"text1": @"Apple7"},
                               @{@"text1": @"Apple8"},
                               ];

              grapesArray = @[ @{@"text2": @"Grapes1"},
                               @{@"text2": @"Grapes2"},
                               @{@"text2": @"Grapes3"},
                               @{@"text2": @"Grapes4"},
                               @{@"text2": @"Grapes5"},
                               @{@"text2": @"Grapes6"},
                               @{@"text2": @"Grapes7"},
                               @{@"text2": @"Grapes8"},
                               ];



        for (int i=0; i<[sectionTitleArray count]; i++)
        {
            [arrayForBool addObject:[NSNumber numberWithBool:NO]];

        }

    }

    #pragma mark:- UITableView Delegate and Datasource Methods

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return sectionTitleArray.count;
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if ([[arrayForBool objectAtIndex:section] boolValue])
        {
            if (section == 0)
            {
                return [applesArray count];
            }
            else if (section == 1)
            {
                return [grapesArray count];
            }
            }
        return 1;
    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {
        static NSString *cellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        cell.textLabel.textColor = [UIColor purpleColor];

        if (cell == nil)
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        BOOL manyCells = [[arrayForBool objectAtIndex:indexPath.section]boolValue];

        if (!manyCells)
        {
            cell.backgroundColor = [UIColor clearColor];
            cell.textLabel.text = @"";
        }

        else
        {
            if (indexPath.section == 0)
            {
                cell.textLabel.text = [[applesArray objectAtIndex:indexPath.row]objectForKey:@"text1"];
            }
            else if (indexPath.section == 1)
            {
                cell.textLabel.text = [[grapesArray objectAtIndex:indexPath.row]objectForKey:@"text2"];
            }

        }
        return cell;

    }

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        /*************** Close the section, once the data is selected ***********************************/
        [arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];

        [_tablevw reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([[arrayForBool objectAtIndex:indexPath.section] boolValue])
        {
            return 40;
        }
        return 0;
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 40;
    }

    #pragma mark - Creating View for TableView Section

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView *sectionView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 280,40)];
        sectionView.tag=section;
        UILabel *viewLabel=[[UILabel alloc]initWithFrame:CGRectMake(13, 0, _tablevw.frame.size.width-10, 40)];
        viewLabel.backgroundColor=[UIColor clearColor];
        viewLabel.font=[UIFont systemFontOfSize:15];
        viewLabel.text= [[sectionTitleArray objectAtIndex:section] objectForKey:@"text"];
        viewLabel.textColor=[UIColor purpleColor];
        [sectionView addSubview:viewLabel];

        /********** Add a custom Separator with Section view *******************/
        UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(12, 40, _tablevw.frame.size.width-35, 1)];
        separatorLineView.backgroundColor = [UIColor purpleColor];
        [sectionView addSubview:separatorLineView];

        button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"Add_plus_iCon_iphone_4s"] forState:UIControlStateNormal];
        button.frame = CGRectMake(sectionView.frame.size.width-10, 10, 20, 20);
        [sectionView addSubview:button];

        /********** Add UITapGestureRecognizer to SectionView   **************/

        UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
        [sectionView addGestureRecognizer:headerTapped];
        return  sectionView;

    }

    #pragma mark - Table header gesture tapped

    - (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];

        if (indexPath.row == 0)
        {
            BOOL collapsed  = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
            for (int i=0; i<[sectionTitleArray count]; i++)
            {
                if (indexPath.section==i)
                {
                    [arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:!collapsed]];
                }
            }
            [_tablevw reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
        }

        if (clickedIndex == 0)
        {
            clickedIndex++;
            [button setImage:[UIImage imageNamed:@"Add_minus_iCon_iphone_4s"] forState:UIControlStateNormal];
        }

        else
        {
            clickedIndex  = 0;
            [button setImage:[UIImage imageNamed:@"Add_plus_iCon_iphone_4s"] forState:UIControlStateNormal];

        }

        [_tablevw reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }

    -(IBAction)minusBtnAction:(id)sender
    {
        if (clickedIndex == 0)
        {
            clickedIndex++;
            [_minusBtn setImage:[UIImage imageNamed:@"ic_minus"] forState:UIControlStateNormal];
        }
        else
        {
            clickedIndex = 0;
            [_minusBtn setImage:[UIImage imageNamed:@"ic_plus"] forState:UIControlStateNormal];

        }
    }

我点击+按钮时tableview中有两行,applesArray中的对象正在展开和折叠。默认情况下,+符号将显示在每个单元格上。如果我点击第一个按钮将更改为 - 并且tableview单元格将展开。

在我的导航栏中使用相同的方式,我有ic_minus图像和按钮背景。当按钮处于ic_minus状态时,所有部分都应显示正常,即应显示苹果和葡萄。

之后,当我点击ic_minus按钮时,它应该变成ic_plus图像,并且所有部分都应该扩展,它们的行对象如苹果与苹果1,苹果2 ...... apple8应该扩展,与grapeArray一样。< / p>

0 个答案:

没有答案