仅使用一个tableview,可扩展单元格

时间:2015-04-06 12:13:00

标签: ios uitableview

我有一个tableview,我有4个字段(个人信息,教育,爱好,兴趣)。现在我的要求是当我点击一个单元格时,该单元格将会扩展,并且该单元格的细节将显示出来。

作为示例,如果我点击“个人信息”单元格,该单元格将展开,并且将显示的数据是名字,姓氏等。

我只使用一个tableview就可以做到这一点。

任何人都可以告诉我如何做到这一点。任何帮助都非常感谢。

2 个答案:

答案 0 :(得分:0)

以下是代码。只需很少的修改,您就可以完全满足您的要求。

NSArray *tableData;
static int selectedSection=10;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
      isValueSelected=NO;
    tableData=[[NSArray alloc]initWithObjects:@"Sivajee",@"Sri Ramana",@"Anusha",@"Hari Krishna",@"Manojna",@"Vamsi", nil];


    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    int numberofRows=0;
    NSLog(@"%d",selectedSection);

    if(selectedSection!=section)
            {
                return 0;
                //numberofRows=[tableData count];

            }
            else
            {
                numberofRows=[tableData count];
                return numberofRows;
                //numberofRows=0;

            }

    //return numberofRows;

}

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


    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];

    if(cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }


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



    return cell;



}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return [tableData count];
}
//
/*
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    return tableData;
}*/


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    [view setBackgroundColor:[UIColor redColor]];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom ];
    [button setTag:section];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:[tableData objectAtIndex:section]  forState:UIControlStateNormal];
    [button setFrame:CGRectMake(100, 0, 100, 20)];
    [view addSubview:button];

    return view;
}

-(void)buttonClicked:(id)sender
{
    UIButton *but=(UIButton *)sender;
    NSLog(@"sender tag is %d",but.tag);
    isValueSelected=YES;
    selectedSection=but.tag;
    NSLog(@"%d",selectedSection);
    [self.tblControll reloadData];


}

答案 1 :(得分:0)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(selectedIndex == indexPath.row)
{
    expanted row hight
}
else
{
    normal row hight
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(selectedIndex == indexPath.row)
{
    show all the label you want to show
}
else
{
    hide the label you have to hid when in the normal position.
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectedIndex = indexPath.row;
         [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}