iOS中tableview的展开折叠方法 [英] Expand and collapse method for tableview in iOS

查看:40
本文介绍了iOS中tableview的展开折叠方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含 2 个部分的 tableview,并在 tableview 中显示了数据数组..现在我想展开和折叠该部分...我只是一个初学者,请提供任何示例代码来展开和折叠 tableview 部分......

I created one tableview with 2 section and i displayed the array of the data in the tableview .. now i want to expand and collapse the section… i am just a beginner please give any sample code for expand and collapse the tableview section…

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellitem = @"simpletableitem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellitem];

    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellitem];
    }

    cell.textLabel.text = [arraytable objectAtIndex:[indexPath row]];

    return cell;
}

这是我的 cellForRowAtIndexPath 和 arraytable 是我的数组我给它在视图中加载

This is my cellForRowAtIndexPath and arraytable is my array i give it in view did load

arraytable = @[@"hari12",@"narayanan",@"Praba",@"Deepak",@"Sailesh",@"Ram charan"]; 

推荐答案

这是我为实现您的需求而编写的示例代码.

Here's a sample code I've made to achieve what you want.

您可以将其用作一般准则,因为除了我所做的之外,还有很多方法可以实现这一点.

You can use this as a general guideline, since there are plenty of ways to achieve this, other than what I've done.

我从上面的示例中注意到,您有一个包含 2 个部分的表视图,因此这就是我将用于示例代码的内容.

I've noticed from your example above that you have a table view with 2 sections, so this is what I'll use for the sample code.

也许有更好的方法来实现以下内容,但这是我的首要任务,我认为它非常简单明了.
我还在下面的代码中添加了解释作为注释.
请注意,您可能需要更改一些变量的名称以适合您的代码(例如 self.tableView,如果您使用其他内容)和 section1DataArray、section2DataArray

There are maybe better ways to implement the below, but that was at the top of my head, and I think it's pretty simple and straightforward.
I've also included explanations as comments in the code below.
Note that you'll probably need to change some variables' names to fit your code (such as self.tableView, if you use something else) and section1DataArray, section2DataArray

// I've declared 2 BOOL properties to hold whether each section is visible or not  
@interface ViewController ()  
@property (nonatomic) BOOL section1visible, section2visible;  
@end  

-(void)viewDidLoad {  
    // After creating the table view, I've set the footer view frame to CGRectZero,  
    // so when a table view is collapsed you won't have the table columns 'bounds'  
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame: CGRectZero];  
}  

// Then I've created a custom header for each table since I've wanted to make the header  
// name a button which collapse/expand the table view.  
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {  
    // Here I've set the height arbitrarily to be 50 points  
    return 50;  
}  

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {  
    // Change the below frame to fit your needs. In my example, I've used a table view  
    // to be at the width of the screen, and the height of 50 points (as we've set above)
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];  
    // Then create a button  
    // I've arbitrarily chosen a size of 100x20 and created a frame to be placed in the  
    // middle of the above header
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(header.frame.size.width/2 - 50, header.frame.size.height/2 - 10, 100, 20)];  

    // Now I'm setting the tag (for later use) and title of each button  
    if(section == 0) { // First section  
        [button setTitle:@"Section 1" forState:UIControlStateNormal];  
        button.tag = 0;  
    } else { // Else, second section, since we only have two  
        [button setTitle:@"Section 2" forState:UIControlStateNormal];  
        button.tag = 1;  
    }  

    // I've arbitrarily chose to set the button colour to gray colour  
    button.titleLabel.textColor = [UIColor grayColor];  
    // Then we need to actually add an action to the button  
    [button addTarget:self action:@selector(updateTableView:) forControlEvents:UIControlEventTouchUpInside];  
    // Then we need to add the button to the header view itself  
    [header addSubview:button];  

    return header;  
}  

// Then we need to update our tableView:numberOfRowsInSection: to check for our BOOL value  
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
    if(section == 0 && self.section1visible) {  
        return [self.section1DataArray count];  
    } else if (section == 1 && self.section2visible) {  
        return [self.section2DataArray count];  
    } else {  
        return 0;  
    }  
}  

// Then we need to create the actual method the button calls  
-(void)updateTableView:(UIButton *)sender {  
    // Check the button tag, to identify which header button was pressed  
    NSInteger section = sender.tag;  

    if(section == 0) {  
        self.section1visible = !self.section1visible;  
    } else { // section == 1  
        self.section2visible = !self.section2visible;  
    }  

    // Use an animation to update the UI to create a 'smooth' expand/collapse  
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];  
}  

祝你好运.

这篇关于iOS中tableview的展开折叠方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆