In Table view How to add another custom cell in expanded cell in ios?

你。 提交于 2019-12-20 05:14:00

问题


Hi i have been working expanding cell for the past few days from some suggestion of internet i found code to expand the customcell. Now my question is i am using custom cell here so after expanding cell i wanna add another custom cell in the expanded custom cell. So any one can help me to do this??? thanks in advance

@interface MyHomeView ()
{
    NSIndexPath *selectedindexpath;

}
@end


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

}

This how am increasing the table Height

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if(selectedindexpath!=nil &&[selectedindexpath compare:indexPath]==NSOrderedSame)
    {

               return 140;
    }


    return 52;

}

回答1:


Here is a Apple's sample code Sample code for submenu

This sample code shows you how to construct submenu using UITableView. Basic idea is menu item will be a section of table and submenu item will be a row of respective section.




回答2:


For this submenus refer what @Iducool posted answer. For your question -> "In Table view How to add another custom cell in expanded cell in ios?" for this you better need to add another UITableView inside the expanded cell so that you can manage them easily following example gives some idea to put UITableView within the Custom UITableView cell

   //in your main Table view 

  #import "ViewController.h"
  #import "CustomCell.h"
  @interface ViewController ()<UITableViewDataSource , UITableViewDelegate>

  @end

  @implementation ViewController

  - (void)viewDidLoad
 {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
 }

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

  - (void)dealloc 
  {
    [_aTV release];
    [super dealloc];
  }


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

  -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
      return 3;
    }

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
   CustomCell *cell = [self.aTV dequeueReusableCellWithIdentifier:@"Cell"];
   if(cell == nil)
     {
        cell = [[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]autorelease];
     }

   cell.dataAraay = [NSMutableArray arrayWithObjects:@"subMenu->1",@"subMenu->2",@"subMenu->3",@"subMenu->4",@"subMenu->5", nil];
   return  cell;
  }

  -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  {
     return 150;
  }


   //in your custom tableview cell 
   //  .m file
   #import "CustomCell.h"

   @implementation CustomCell 
   @synthesize dataAraay; //array to hold submenu data

   - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  {
     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
     if (self) {
    // Initialization code
     self.frame = CGRectMake(0, 0, 300, 50);
     UITableView *subMenuTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain]; //create tableview a

      subMenuTableView.tag = 100;
      subMenuTableView.delegate = self;
      subMenuTableView.dataSource = self;
      [self addSubview:subMenuTableView]; // add it cell
      [subMenuTableView release]; // for without ARC
   }
    return self;
 }

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
 {
     [super setSelected:selected animated:animated];

      // Configure the view for the selected state
 }

 -(void)layoutSubviews
  {
     [super layoutSubviews];
     UITableView *subMenuTableView =(UITableView *) [self viewWithTag:100];
     subMenuTableView.frame = CGRectMake(0.2, 0.3, self.bounds.size.width-5, self.bounds.size.height-5);//set the frames for tableview

  }

 //manage datasource and  delegate for submenu tableview
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
     return 1;
  }

   -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
   {
       return dataAraay.count;
   }

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
     if(cell == nil)
      {
         cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"]autorelease];
      }
    cell.textLabel.text = [self.dataAraay objectAtIndex:indexPath.row];

    return cell;

 }

@end


Hope this helps :)



来源:https://stackoverflow.com/questions/18266838/in-table-view-how-to-add-another-custom-cell-in-expanded-cell-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!