1

嗨,过去几天我一直在根据互联网的一些建议来扩展单元格,我找到了扩展自定义单元格的代码。现在我的问题是我在这里使用自定义单元格,所以在扩展单元格后,我想在扩展的自定义单元格中添加另一个自定义单元格。所以任何人都可以帮助我做到这一点???提前致谢

@interface MyHomeView ()
{
    NSIndexPath *selectedindexpath;

}
@end


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

}

这如何增加桌子高度

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

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

               return 140;
    }


    return 52;

}
4

2 回答 2

0

对于此子菜单,请参阅 @Iducool 发布的答案。对于您的问题->“在表格视图中如何在 ios 的扩展单元格中添加另一个自定义单元格?” 为此,您最好需要在展开的单元格中添加另一个 UITableView,以便您可以轻松管理它们,下面的示例给出了将 UITableView 放在自定义 UITableView 单元格中的一些想法

   //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


希望这可以帮助 :)

于 2013-08-16T07:30:27.997 回答
0

这是Apple的示例代码 子菜单的示例代码

此示例代码向您展示了如何使用 UITableView 构建子菜单。基本思想是菜单项将是表格的一部分,子菜单项将是相应部分的一行。

于 2013-08-16T06:25:21.490 回答