1

我想在 tableview 的选定单元格上添加一个视图。我正在写这段代码。

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

  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  UIView *myView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
  myView.backgroundColor=[UIColor greenColor];
  myView.tag=1001;
  [cell.contentView addSubview:myView];
}

编写此代码后,由于此视图,我的其他单元格未显示。我想将选定单元格的高度增加到 200 并在选定单元格上添加我的自定义视图。

谁能告诉我如何做到这一点,提前谢谢。

4

3 回答 3

0

好的,正如我告诉您更好的那样,您需要在以下示例中对UaiteViewCell进行子类别,在选择单元格时,它会将自定义视图添加到选定的单元格中,您会通过此详细信息进行此操作,并根据您的要求将其更改。希望这对你有帮助:)


 //in subclassed cell "CustomCell.h" file
  @interface CustomCell : UITableViewCell
 {
    UIView *aView; //to hold ur view
    BOOL addView;  //to check wheather to add view or not 
 }
 @property(nonatomic, retain) UIView *aView;
 @property(nonatomic, assign) BOOL addView;
 @end

//in "CustomCell.m" file
#import "CustomCell.h"

@implementation CustomCell

@synthesize aView;
@synthesize addView;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if (self) {
    // Initialization code
     aView = [[UIView alloc]initWithFrame:CGRectZero]; //init the view with zero rect as if u want initially frame is zero
     aView.backgroundColor = [UIColor greenColor]; //to know weather it is added or not 

     [self addSubview:aView];//added to cell

   }
   return self;
 }
  - (void)dealloc
 {
  [aView release];
  [super dealloc];
 }


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

   // Configure the view for the selected state
  }

  - (void)layoutSubviews
  {

     [super layoutSubviews];
      if(self.addView) //if the view is added
      {
         self.aView.frame = CGRectMake(10, 3, 100,200);
      }
       else
      {
         self.aView.frame = CGRectZero;//if there is no view
      }


  }


  //in your ViewController.h file
  @interface ViewController : UIViewController<CountdownTimerDelegate>
  @property (retain, nonatomic) IBOutlet UITableView *aTableView;
  @property (nonatomic, retain) NSIndexPath *selectedIndexPath;
  @end

  //in ViewController.m file


  #import"CustomCell" //add this to header file
   -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {

       static NSString *CellIdentifier = @"Cell";
       CustomCell *cell = [self.aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if(cell == nil)
      {
          cell =[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      }
      if(self.selectedIndexPath != nil)
       {
          if(self.selectedIndexPath.row == indexPath.row)
           cell.addView = YES;
          else
          cell.addView = NO;
       }


    //initially set it no for all cell

    return cell;
  }

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    //in did select row at indexpath

    self.selectedIndexPath = indexPath;
    [self.aTableView reloadData]; 
 }


 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if(self.selectedIndexPath != nil)
   {
    if(self.selectedIndexPath.row == indexPath.row)
     {
        return 200;
     }
      return 45;
   }
   else
     return 45;

  }


于 2013-09-27T13:43:47.697 回答
0

创建选定单元格的属性

@property (nonatomic) int currentSelection;

然后在这里初始化

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.currentSelection = -1;
}

在 heightForRowAtIndexPath 中,您可以为所选单元格设置所需的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    int rowHeight;
    if ([indexPath row] == self.currentSelection) {
        rowHeight = 200;
    }
    else 
      rowHeight = CURRENT_HEIGHT_HERE;
    return rowHeight;
}

然后在此处添加您的视图并将其隐藏为:

// 自定义表格视图单元格的外观。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    }

                UIView *myView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
                myView.backgroundColor=[UIColor greenColor];
                myView.tag=1001;
    [myView setHidden:YES];

                [cell.contentView addSubview:myView];
    // Configure the cell.
    return cell;
}

在 didSelectRow 中,您保存当前选择并保存动态高度(如果需要)

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
            // do things with your cell here

            // set selection
            self.currentSelection = indexPath.row;

            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                [[cell viewWithTag:1001] setHidden:NO];

            // animate
            [tableView beginUpdates];
            [tableView endUpdates];
    }
于 2013-09-27T12:29:40.193 回答
0
- (void)tableView:(UITableView *)tableView  didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     selectedIndex = indexPath.row;
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
     myView.backgroundColor = [UIColor greenColor];
     myView.tag = 1001;
     [cell.contentView addSubview:myView];
     [tableView reloadData];
}

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

    if ([indexPath row] == selectedIndex) {
        return myView.bound.size.height;
    }
    else 
        return currentHeight;
}

希望这会奏效。

于 2013-09-27T18:03:09.857 回答